package_spec.rb 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. require 'rails_helper'
  2. RSpec.describe 'Packages', type: :request do
  3. let(:admin) do
  4. create(:admin)
  5. end
  6. let(:agent) do
  7. create(:agent)
  8. end
  9. let(:customer) do
  10. create(:customer)
  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(:forbidden)
  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 required')
  19. end
  20. it 'does packages index with admin' do
  21. authenticated_as(admin)
  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, 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('Invalid BasicAuth credentials')
  33. end
  34. it 'does packages index with inactive admin' do
  35. admin = create(:admin, active: false, password: 'we need a password here')
  36. authenticated_as(admin)
  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('Invalid BasicAuth credentials')
  41. end
  42. it 'does packages index with agent' do
  43. authenticated_as(agent)
  44. get '/api/v1/packages', as: :json
  45. expect(response).to have_http_status(:forbidden)
  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)
  52. get '/api/v1/packages', as: :json
  53. expect(response).to have_http_status(:forbidden)
  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