summaryrefslogtreecommitdiff
path: root/spec/requests/products_controller/create_spec.rb
blob: 4d6a9bc9e887f694cb378a3c73ead6a9e9f2169d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'POST /api/products', type: :request do
  let(:product) { build(:product) }
  let(:picture) { fixture_file_upload('800px-Stray_cat_on_wall.jpg', 'image/jpeg') }
  let(:categories) do
    c = ''
    product.categories.each { |category| c += "#{category}," }
    c
  end

  it_behaves_like 'a POST request' do
    let(:user) { create(:user_account, role: 'master') }
    let(:token) { jwt(user) }
    let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } }
    let(:route) { '/api/products' }
    let(:expected_error_messages) do
      ["can't be blank", 'must be greater than 0', 'must be greater than 0',
       'must be greater than 0', 'must exist']
    end
    let(:desired_error_status) { 422 }
    let(:expected_text) do
      product.categories.concat([product.name, product.unitary_price.to_i.to_s, product.bulk_price.to_i.to_s,
                                 product.available_quantity.to_s, product.company.short_name, 'picture', 'http'])
    end
    let(:params) do
      { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:,
        available_quantity: product.available_quantity, categories:, company_id: product.company_id }
    end
    let(:wrong_params) do
      JSON.generate({ name: '', unitary_price: -10, bulk_price: -10, available_quantity: -10, categories: '',
                      company_id: nil })
    end
  end

  it_behaves_like 'a POST request that requires a master user' do
    let(:user) { create(:user_account, role: 'regular') }
    let(:token) { jwt(user) }
    let(:headers) { { 'CONTENT_TYPE' => 'application/json', 'Authorization' => "Bearer #{token['token']}" } }
    let(:route) { '/api/products' }
    let(:params) do
      { name: product.name, unitary_price: product.unitary_price, bulk_price: product.bulk_price, picture:,
        available_quantity: product.available_quantity, categories:, company_id: product.company_id }
    end
  end
end