123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- # encoding: utf-8
- require 'test_helper'
- class ApiAuthControllerTest < ActionDispatch::IntegrationTest
- setup do
- # set accept header
- @headers = { 'ACCEPT' => 'application/json', 'CONTENT_TYPE' => 'application/json' }
- # create agent
- roles = Role.where(name: %w(Admin Agent))
- groups = Group.all
- UserInfo.current_user_id = 1
- @admin = User.create_or_update(
- login: 'api-admin',
- firstname: 'API',
- lastname: 'Admin',
- email: 'api-admin@example.com',
- password: 'adminpw',
- active: true,
- roles: roles,
- groups: groups,
- )
- # create agent
- roles = Role.where(name: 'Agent')
- @agent = User.create_or_update(
- login: 'api-agent@example.com',
- firstname: 'API',
- lastname: 'Agent',
- email: 'api-agent@example.com',
- password: 'agentpw',
- active: true,
- roles: roles,
- groups: groups,
- )
- # create customer without org
- roles = Role.where(name: 'Customer')
- @customer = User.create_or_update(
- login: 'api-customer1@example.com',
- firstname: 'API',
- lastname: 'Customer1',
- email: 'api-customer1@example.com',
- password: 'customer1pw',
- active: true,
- roles: roles,
- )
- end
- test 'basic auth - admin' do
- admin_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-admin@example.com', 'adminpw')
- Setting.set('api_password_access', false)
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API password access disabled!', result['error'])
- Setting.set('api_password_access', true)
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert(result)
- end
- test 'basic auth - agent' do
- agent_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-agent@example.com', 'agentpw')
- Setting.set('api_password_access', false)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API password access disabled!', result['error'])
- Setting.set('api_password_access', true)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
- assert_response(200)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- end
- test 'basic auth - customer' do
- customer_credentials = ActionController::HttpAuthentication::Basic.encode_credentials('api-customer1@example.com', 'customer1pw')
- Setting.set('api_password_access', false)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API password access disabled!', result['error'])
- Setting.set('api_password_access', true)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
- assert_response(200)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- end
- test 'token auth - admin' do
- admin_token = Token.create(
- action: 'api',
- persistent: true,
- user_id: @admin.id,
- preferences: {
- permission: ['admin.session'],
- },
- )
- admin_credentials = "Token token=#{admin_token.name}"
- Setting.set('api_token_access', false)
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API token access disabled!', result['error'])
- Setting.set('api_token_access', true)
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert(result)
- admin_token.preferences[:permission] = ['admin.session_not_existing']
- admin_token.save!
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('Not authorized (token)!', result['error'])
- admin_token.preferences[:permission] = []
- admin_token.save!
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('Not authorized (token)!', result['error'])
- @admin.active = false
- @admin.save!
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('User is inactive!', result['error'])
- admin_token.preferences[:permission] = ['admin.session']
- admin_token.save!
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('User is inactive!', result['error'])
- @admin.active = true
- @admin.save!
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert(result)
- get '/api/v1/roles', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('Not authorized (token)!', result['error'])
- admin_token.preferences[:permission] = ['admin.session_not_existing', 'admin.role']
- admin_token.save!
- get '/api/v1/roles', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- admin_token.preferences[:permission] = ['ticket.agent']
- admin_token.save!
- get '/api/v1/organizations', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- name = "some org name #{rand(999_999_999)}"
- post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(name, result['name'])
- assert(result)
- name = "some org name #{rand(999_999_999)} - 2"
- put "/api/v1/organizations/#{result['id']}", { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(name, result['name'])
- assert(result)
- admin_token.preferences[:permission] = ['admin.organization']
- admin_token.save!
- get '/api/v1/organizations', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- name = "some org name #{rand(999_999_999)}"
- post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(name, result['name'])
- assert(result)
- name = "some org name #{rand(999_999_999)} - 2"
- put "/api/v1/organizations/#{result['id']}", { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(name, result['name'])
- assert(result)
- admin_token.preferences[:permission] = ['admin']
- admin_token.save!
- get '/api/v1/organizations', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- name = "some org name #{rand(999_999_999)}"
- post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
- assert_response(201)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(name, result['name'])
- assert(result)
- name = "some org name #{rand(999_999_999)} - 2"
- put "/api/v1/organizations/#{result['id']}", { name: name }.to_json, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal(name, result['name'])
- assert(result)
- end
- test 'token auth - agent' do
- agent_token = Token.create(
- action: 'api',
- persistent: true,
- user_id: @agent.id,
- )
- agent_credentials = "Token token=#{agent_token.name}"
- Setting.set('api_token_access', false)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API token access disabled!', result['error'])
- Setting.set('api_token_access', true)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => agent_credentials)
- assert_response(200)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- get '/api/v1/organizations', {}, @headers.merge('Authorization' => agent_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- name = "some org name #{rand(999_999_999)}"
- post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => agent_credentials)
- assert_response(401)
- end
- test 'token auth - customer' do
- customer_token = Token.create(
- action: 'api',
- persistent: true,
- user_id: @customer.id,
- )
- customer_credentials = "Token token=#{customer_token.name}"
- Setting.set('api_token_access', false)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API token access disabled!', result['error'])
- Setting.set('api_token_access', true)
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => customer_credentials)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- get '/api/v1/organizations', {}, @headers.merge('Authorization' => customer_credentials)
- assert_response(200)
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- name = "some org name #{rand(999_999_999)}"
- post '/api/v1/organizations', { name: name }.to_json, @headers.merge('Authorization' => customer_credentials)
- assert_response(401)
- end
- test 'token auth - invalid user - admin' do
- admin_token = Token.create(
- action: 'api',
- persistent: true,
- user_id: @admin.id,
- )
- admin_credentials = "Token token=#{admin_token.name}"
- @admin.active = false
- @admin.save!
- Setting.set('api_token_access', false)
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('API token access disabled!', result['error'])
- Setting.set('api_token_access', true)
- get '/api/v1/sessions', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('User is inactive!', result['error'])
- end
- test 'token auth - expired' do
- Setting.set('api_token_access', true)
- admin_token = Token.create(
- action: 'api',
- persistent: true,
- user_id: @admin.id,
- expires_at: Time.zone.today
- )
- admin_credentials = "Token token=#{admin_token.name}"
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(401)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert_equal('Not authorized (token expired)!', result['error'])
- admin_token.reload
- assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
- end
- test 'token auth - not expired' do
- Setting.set('api_token_access', true)
- admin_token = Token.create(
- action: 'api',
- persistent: true,
- user_id: @admin.id,
- expires_at: Time.zone.tomorrow
- )
- admin_credentials = "Token token=#{admin_token.name}"
- get '/api/v1/tickets', {}, @headers.merge('Authorization' => admin_credentials)
- assert_response(200)
- assert_equal('*', @response.header['Access-Control-Allow-Origin'])
- result = JSON.parse(@response.body)
- assert_equal(Array, result.class)
- assert(result)
- admin_token.reload
- assert_in_delta(admin_token.last_used_at, Time.zone.now, 1.second)
- end
- test 'session auth - admin' do
- post '/api/v1/signin', { username: 'api-admin@example.com', password: 'adminpw', fingerprint: '123456789' }
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- assert_response(201)
- get '/api/v1/sessions', {}
- assert_response(200)
- assert_not(@response.header.key?('Access-Control-Allow-Origin'))
- result = JSON.parse(@response.body)
- assert_equal(Hash, result.class)
- assert(result)
- end
- end
|