Skip to content

Commit

Permalink
Add some tests for workers endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
woarewe committed Apr 4, 2023
1 parent d9e6e18 commit f02088d
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 3 deletions.
2 changes: 1 addition & 1 deletion test/gateways/rest/api/organizations/create_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class REST::API::Organizations::CreateTest < ActionDispatch::IntegrationTest
name = Faker::Company.name

assert_new_organization { execute(name) }
assert_equal saved_organization.name, name
assert_equal name, saved_organization.name
end

test "not creating a new organization with an empty name" do
Expand Down
4 changes: 2 additions & 2 deletions test/gateways/rest/api/organizations/update_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class REST::API::Organizations::UpdateTest < ActionDispatch::IntegrationTest
new_name = Faker::Company.name

assert_no_new_organizations { execute(organization.external_id, new_name) }
assert_equal organization.reload.name, new_name
assert_equal new_name, organization.reload.name
end

test "not updating an organization name to the already existing one" do
Expand All @@ -27,7 +27,7 @@ class REST::API::Organizations::UpdateTest < ActionDispatch::IntegrationTest
create(:organization, name: new_name)

assert_no_new_organizations { execute(organization.external_id, new_name) }
assert_equal organization.reload.name, old_name
assert_equal old_name, organization.reload.name
end

private
Expand Down
49 changes: 49 additions & 0 deletions test/gateways/rest/api/workers/create_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

require "test_helper"

class REST::API::Workers::CreateTest < ActionDispatch::IntegrationTest
include Tests::Helpers::API
include Tests::Helpers::Authentication
include Tests::Helpers::Workers

setup do
@organization = create(:organization)
@manager = create(:worker, :manager, organization: @organization)
@credentials = create_credentials(owner: @manager)
end

test "creating a new worker" do
attributes = valid_worker_attributes

assert_new_worker(@organization) { execute(@organization.external_id, attributes) }

attributes => { first_name:, last_name:, role:, }
assert_equal first_name, saved_worker.first_name
assert_equal last_name, saved_worker.last_name
assert_equal role, saved_worker.role
assert_equal @organization, saved_worker.organization
end

test "not creating a worker with a blank first name" do
attributes = valid_worker_attributes.merge(first_name: "")

assert_no_new_workers(@organization) { execute(@organization.external_id, attributes) }
end

test "not creating a worker with a blank last name" do
attributes = valid_worker_attributes.merge(last_name: "")

assert_no_new_workers(@organization) { execute(@organization.external_id, attributes) }
end

private

def execute(organization_id, attributes)
post(
"/api/workers",
params: { organization_id:, attributes: },
headers: headers_with_auth(@credentials)
)
end
end
32 changes: 32 additions & 0 deletions test/gateways/rest/api/workers/delete_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

require "test_helper"

class REST::API::Workers::DeleteTest < ActionDispatch::IntegrationTest
include Tests::Helpers::API
include Tests::Helpers::Authentication
include Tests::Helpers::Workers

setup do
@organization = create(:organization)
@manager = create(:worker, :manager, organization: @organization)
@credentials = create_credentials(owner: @manager)
end

test "deleting an existing new worker" do
worker = create(:worker, organization: @organization)

execute(worker.external_id)

assert_nil ::Worker.find_by(id: worker.id)
end

private

def execute(worker_id)
delete(
"/api/workers/#{worker_id}",
headers: headers_with_auth(@credentials)
)
end
end
38 changes: 38 additions & 0 deletions test/gateways/rest/api/workers/update_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# frozen_string_literal: true

require "test_helper"

class REST::API::Workers::UpdateTest < ActionDispatch::IntegrationTest
include Tests::Helpers::API
include Tests::Helpers::Authentication
include Tests::Helpers::Workers

setup do
@organization = create(:organization)
@manager = create(:worker, :manager, organization: @organization)
@credentials = create_credentials(owner: @manager)
end

test "updating an existing new worker" do
worker = create(:worker, organization: @organization)
attributes = valid_worker_attributes

assert_no_new_workers(@organization) { execute(worker.external_id, attributes) }

attributes => { first_name:, last_name:, role:, }
worker.reload
assert_equal first_name, worker.first_name
assert_equal last_name, worker.last_name
assert_equal role, worker.role
end

private

def execute(worker_id, attributes)
put(
"/api/workers/#{worker_id}",
params: { attributes: },
headers: headers_with_auth(@credentials)
)
end
end
1 change: 1 addition & 0 deletions test/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ module Helpers; end
require_relative "helpers/authentication"
require_relative "helpers/shifts"
require_relative "helpers/organizations"
require_relative "helpers/workers"
require_relative "helpers/api"
40 changes: 40 additions & 0 deletions test/helpers/workers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module Tests
module Helpers
module Workers
def valid_worker_attributes
first_name = Faker::Name.first_name
last_name = Faker::Name.last_name
role = ::Worker::ROLES.to_a.sample
time_zone = TZInfo::Timezone.all.sample.name

{ first_name:, last_name:, role:, time_zone: }
end

def assert_no_new_workers(organization, &)
assert_no_changes(
-> { workers_count(organization) },
&
)
end

def assert_new_worker(organization, &)
assert_changes(
-> { workers_count(organization) },
from: workers_count(organization),
to: workers_count(organization) + 1,
&
)
end

def saved_worker
::Worker.find_by(external_id: response_body.fetch(:id))
end

def workers_count(organization)
::Worker.where(organization:).count
end
end
end
end

0 comments on commit f02088d

Please sign in to comment.