require_relative '../test_config' require 'helix_versioning_engine' describe 'HelixVersioningEngine users' do include Rack::Test::Methods def app HELIX_WEB_SERVICES_APP end rand_str = (0...8).map { (65 + rand(26)).chr }.join new_user = { 'User' => "test_#{rand_str}", 'FullName' => "Test User #{rand_str}", 'Email' => "test_#{rand_str}@example.com" } context 'GET /helix_versioning_engine/v78/users' do it 'should list jdoe and super users' do authorize 'super', ticket_for_super get('/helix_versioning_engine/v78/users') expect(last_response.status).to eq(200) users = JSON.parse(last_response.body) expect(users.map { |x| x['User'] }).to include('jdoe') expect(users.map { |x| x['User'] }).to include('super') end end context 'GET /helix_versioning_engine/v78/users/:user' do it 'should return the user mmustermann' do authorize 'super', ticket_for_super get('/helix_versioning_engine/v78/users/mmustermann') expect(last_response.status).to eq(200) user = JSON.parse(last_response.body) expect(user['User']).to eq('mmustermann') expect(user['FullName']).to eq('Max Mustermann') end end context 'POST /helix_versioning_engine/v78/users' do it 'should create a new user' do authorize 'super', ticket_for_super post('/helix_versioning_engine/v78/users', new_user) expect(last_response.status).to eq(200) get("/helix_versioning_engine/v78/users/#{new_user['User']}") expect(last_response.status).to eq(200) user = JSON.parse(last_response.body) expect(user['FullName']).to eq(new_user['FullName']) end end context 'PATCH /helix_versioning_engine/v78/users/:user' do it 'should update a user email' do authorize 'super', ticket_for_super patch('/helix_versioning_engine/v78/users/mmustermann', {'Email' => "max_power_#{rand_str}@example.com"}) expect(last_response.status).to eq(200) get('/helix_versioning_engine/v78/users/mmustermann') expect(last_response.status).to eq(200) user = JSON.parse(last_response.body) expect(user['Email']).to eq("max_power_#{rand_str}@example.com") end it 'should return a 404 if you attempt to patch an invalid user' do authorize 'super', ticket_for_super patch('/helix_versioning_engine/v78/users/invalid', {'Email' => "max_power_#{rand_str}@example.com"}) expect(last_response.status).to eq(404) end end context 'DELETE /helix_versioning_engine/v78/users/:user' do it 'should delete the user' do authorize 'super', ticket_for_super delete("/helix_versioning_engine/v78/users/#{new_user['User']}") expect(last_response.status).to eq(200) get("/helix_versioning_engine/v78/users/#{new_user['User']}") expect(last_response.status).to eq(404) end end end