package_spec.rb 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. RSpec.describe 'Packages', type: :request do
  3. let(:admin_user) do
  4. create(:admin_user)
  5. end
  6. let(:agent_user) do
  7. create(:agent_user)
  8. end
  9. let(:customer_user) do
  10. create(:customer_user)
  11. end
  12. describe 'request handling' do
  13. it 'does packages index with nobody' do
  14. get '/api/v1/packages', as: :json
  15. expect(response).to have_http_status(:unauthorized)
  16. expect(json_response).to be_a_kind_of(Hash)
  17. expect(json_response['packages']).to be_falsey
  18. expect(json_response['error']).to eq('authentication failed')
  19. end
  20. it 'does packages index with admin' do
  21. authenticated_as(admin_user)
  22. get '/api/v1/packages', as: :json
  23. expect(response).to have_http_status(:ok)
  24. expect(json_response).to be_a_kind_of(Hash)
  25. expect(json_response['packages']).to be_truthy
  26. end
  27. it 'does packages index with admin and wrong pw' do
  28. authenticated_as(admin_user, password: 'wrongadminpw')
  29. get '/api/v1/packages', as: :json
  30. expect(response).to have_http_status(:unauthorized)
  31. expect(json_response).to be_a_kind_of(Hash)
  32. expect(json_response['error']).to eq('authentication failed')
  33. end
  34. it 'does packages index with inactive admin' do
  35. admin_user = create(:admin_user, active: false, password: 'we need a password here')
  36. authenticated_as(admin_user)
  37. get '/api/v1/packages', as: :json
  38. expect(response).to have_http_status(:unauthorized)
  39. expect(json_response).to be_a_kind_of(Hash)
  40. expect(json_response['error']).to eq('authentication failed')
  41. end
  42. it 'does packages index with agent' do
  43. authenticated_as(agent_user)
  44. get '/api/v1/packages', as: :json
  45. expect(response).to have_http_status(:unauthorized)
  46. expect(json_response).to be_a_kind_of(Hash)
  47. expect(json_response['packages']).to be_falsey
  48. expect(json_response['error']).to eq('Not authorized (user)!')
  49. end
  50. it 'does packages index with customer' do
  51. authenticated_as(customer_user)
  52. get '/api/v1/packages', as: :json
  53. expect(response).to have_http_status(:unauthorized)
  54. expect(json_response).to be_a_kind_of(Hash)
  55. expect(json_response['packages']).to be_falsey
  56. expect(json_response['error']).to eq('Not authorized (user)!')
  57. end
  58. end
  59. end