From 9f018bcfb9725e1e854b584a429ad1cc8da4bb45 Mon Sep 17 00:00:00 2001 From: Jannik Pulfer Date: Tue, 30 Jul 2024 10:32:50 +0200 Subject: [PATCH] Update tests with new helpers --- app/domain/ptime/people_employees.rb | 10 ++-- spec/domain/ptime/people_employees_spec.rb | 13 +---- spec/features/people_employees_spec.rb | 61 ++-------------------- spec/fixtures/files/json/wally.json | 23 ++++++++ 4 files changed, 32 insertions(+), 75 deletions(-) create mode 100644 spec/fixtures/files/json/wally.json diff --git a/app/domain/ptime/people_employees.rb b/app/domain/ptime/people_employees.rb index 96a2e645e..125534e6d 100644 --- a/app/domain/ptime/people_employees.rb +++ b/app/domain/ptime/people_employees.rb @@ -22,18 +22,18 @@ def update_person_data(person) begin raise 'Person has no ptime_employee_id' unless person.ptime_employee_id - ptime_employee = Ptime::Client.new.get("employees/#{person.ptime_employee_id}")['data'] - rescue RestClient::NotFound + ptime_employee = Ptime::Client.new.request(:get, "employees/#{person.ptime_employee_id}") + rescue CustomExceptions::PTimeClientError raise "Ptime_employee with ptime_employee_id #{person.ptime_employee_id} not found" else - ptime_employee['attributes'].each do |key, value| + ptime_employee[:attributes].each do |key, value| if key.to_sym.in?(attribute_mapping.keys) person[attribute_mapping[key.to_sym]] = (value.presence || '-') end end - ptime_employee_employed = ptime_employee['attributes']['is_employed'] + ptime_employee_employed = ptime_employee[:attributes][:is_employeed] person.company = Company.find_by(name: ptime_employee_employed ? 'Firma' : 'Ex-Mitarbeiter') - ptime_employee_nationalities = ptime_employee['attributes']['nationalities'] + ptime_employee_nationalities = ptime_employee[:attributes][:nationalities] person.nationality = ptime_employee_nationalities[0] person.nationality2 = ptime_employee_nationalities[1] person.save! diff --git a/spec/domain/ptime/people_employees_spec.rb b/spec/domain/ptime/people_employees_spec.rb index b1cc07ab9..e8a9a5a82 100644 --- a/spec/domain/ptime/people_employees_spec.rb +++ b/spec/domain/ptime/people_employees_spec.rb @@ -1,15 +1,6 @@ require 'rails_helper' describe Ptime::PeopleEmployees do - ptime_base_test_url = "www.ptime.example.com" - ptime_api_test_username = "test username" - ptime_api_test_password = "test password" - before(:each) do - ENV["PTIME_BASE_URL"] = ptime_base_test_url - ENV["PTIME_API_USERNAME"] = ptime_api_test_username - ENV["PTIME_API_PASSWORD"] = ptime_api_test_password - end - it 'should raise error when no ptime_employee_id is passed to new action' do expect{ Ptime::PeopleEmployees.new.create_or_find(nil) }.to raise_error(RuntimeError, 'No ptime_employee_id provided') end @@ -24,9 +15,7 @@ end it 'should raise error when person is not found in ptime api' do - stub_request(:get, "#{ptime_base_test_url}/api/v1/employees/50"). - to_return(headers: { 'content-type': "application/vnd.api+json; charset=utf-8" }, status: 404) - .with(basic_auth: [ptime_api_test_username, ptime_api_test_password]) + stub_ptime_request('', "employees/50", 404) person_wally = people(:wally) person_wally.ptime_employee_id = 50 diff --git a/spec/features/people_employees_spec.rb b/spec/features/people_employees_spec.rb index ea88eb471..6900e6840 100644 --- a/spec/features/people_employees_spec.rb +++ b/spec/features/people_employees_spec.rb @@ -1,46 +1,13 @@ require 'rails_helper' describe Ptime::PeopleEmployees do - ptime_base_test_url = "www.ptime.example.com" - ptime_api_test_username = "test username" - ptime_api_test_password = "test password" - before(:each) do - ENV["PTIME_BASE_URL"] = ptime_base_test_url - ENV["PTIME_API_USERNAME"] = ptime_api_test_username - ENV["PTIME_API_PASSWORD"] = ptime_api_test_password - end - describe 'Update or create person', type: :feature, js: true do before(:each) do sign_in auth_users(:user), scope: :auth_user end it 'should update person when visited' do - updated_wally = { - 'data': { - 'id': 50, - 'type': 'employee', - 'attributes': { - 'shortname': 'CAL', - 'firstname': 'Changed Wally', - 'lastname': 'Allround', - 'full_name': 'Changed Wally Allround', - 'email': 'changedwally@example.com', - 'marital_status': 'single', - 'nationalities': %w[DE DK], - 'graduation': 'Quarter-Stack Developer', - 'department_shortname': 'SYS', - 'employment_roles': [], - 'is_employed': false, - 'birthdate': '1.1.2000', - 'location': 'Basel', - }} - } - - - stub_request(:get, "#{ptime_base_test_url}/api/v1/employees/50"). - to_return(body: updated_wally.to_json, headers: { 'content-type': "application/vnd.api+json; charset=utf-8" }, status: 200) - .with(basic_auth: [ptime_api_test_username, ptime_api_test_password]) + stub_ptime_request(fixture_data("wally").to_json, "employees/50", 200) Company.create!(name: "Ex-Mitarbeiter") person_wally = people(:wally) @@ -63,30 +30,8 @@ end it 'should create person when visited' do - updated_wally = { - 'data': { - 'id': 50, - 'type': 'employee', - 'attributes': { - 'shortname': 'CAL', - 'firstname': 'Changed Wally', - 'lastname': 'Allround', - 'full_name': 'Changed Wally Allround', - 'email': 'changedwally@example.com', - 'marital_status': 'single', - 'nationalities': %w[DE EG], - 'graduation': 'Quarter-Stack Developer', - 'department_shortname': 'SYS', - 'employment_roles': [], - 'is_employed': true, - 'birthdate': '1.1.2000', - 'location': 'Basel', - }} - } + stub_ptime_request(fixture_data("wally").to_json, "employees/50", 200) - stub_request(:get, "#{ptime_base_test_url}/api/v1/employees/50"). - to_return(body: updated_wally.to_json, headers: { 'content-type': "application/vnd.api+json; charset=utf-8" }, status: 200) - .with(basic_auth: [ptime_api_test_username, ptime_api_test_password]) Company.create!(name: "Ex-Mitarbeiter") person_wally = people(:wally) @@ -102,7 +47,7 @@ expect(new_wally.birthdate).to eq('1.1.2000') expect(new_wally.location).to eq('Basel') expect(new_wally.nationality).to eq('DE') - expect(new_wally.nationality2).to eq('EG') + expect(new_wally.nationality2).to eq('DK') end end end \ No newline at end of file diff --git a/spec/fixtures/files/json/wally.json b/spec/fixtures/files/json/wally.json new file mode 100644 index 000000000..582642fcd --- /dev/null +++ b/spec/fixtures/files/json/wally.json @@ -0,0 +1,23 @@ +{ + "data": { + "id": 50, + "type": "employee", + "attributes": { + "shortname": "CAL", + "firstname": "Changed Wally", + "lastname": "Allround", + "full_name": "Changed Wally Allround", + "email": "changedwally@example.com", + "marital_status": "single", + "nationalities": [ + "DE", "DK" + ], + "graduation": "Quarter-Stack Developer", + "department_shortname": "SYS", + "employment_roles2": [], + "is_employed": false, + "birthdate": "1.1.2000", + "location": "Basel" + } + } +} \ No newline at end of file