Skip to content

Commit

Permalink
Update tests with new helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
RandomTannenbaum committed Jul 30, 2024
1 parent 7037d7f commit 9f018bc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 75 deletions.
10 changes: 5 additions & 5 deletions app/domain/ptime/people_employees.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down
13 changes: 1 addition & 12 deletions spec/domain/ptime/people_employees_spec.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
61 changes: 3 additions & 58 deletions spec/features/people_employees_spec.rb
Original file line number Diff line number Diff line change
@@ -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': '[email protected]',
'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)
Expand All @@ -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': '[email protected]',
'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)
Expand All @@ -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
23 changes: 23 additions & 0 deletions spec/fixtures/files/json/wally.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"data": {
"id": 50,
"type": "employee",
"attributes": {
"shortname": "CAL",
"firstname": "Changed Wally",
"lastname": "Allround",
"full_name": "Changed Wally Allround",
"email": "[email protected]",
"marital_status": "single",
"nationalities": [
"DE", "DK"
],
"graduation": "Quarter-Stack Developer",
"department_shortname": "SYS",
"employment_roles2": [],
"is_employed": false,
"birthdate": "1.1.2000",
"location": "Basel"
}
}
}

0 comments on commit 9f018bc

Please sign in to comment.