Skip to content

Commit

Permalink
refactor mock_login_gov
Browse files Browse the repository at this point in the history
  • Loading branch information
stepchud committed Sep 17, 2024
1 parent 0aea978 commit 3c990e0
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 34 deletions.
27 changes: 4 additions & 23 deletions spec/requests/sessions_request_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,9 @@
end

it "get /auth/result successful" do
user = User.new(email: "[email protected]", token: SecureRandom.uuid)
code = "ABC123"
login_gov = instance_double(LoginGov)
allow(LoginGov).to receive(:new).and_return(login_gov)
allow(login_gov).to receive(:exchange_token_from_auth_result).with(code).and_return(
[{ email: "[email protected]", sub: "sub" }]
)

# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(SessionsController).to(
receive(:send_user_jwt_to_phoenix).with(instance_of(String)).and_return(true)
)
# rubocop:enable RSpec/AnyInstance
mock_login_gov(user, code)

get "/auth/result", params: { code: }
expect(response).to have_http_status(:redirect)
Expand All @@ -54,20 +45,10 @@
email = "[email protected]"
token = SecureRandom.uuid

User.create!({ email:, token: })
user = User.create!({ email:, token: })

code = "ABC123"
login_gov = instance_double(LoginGov)
allow(LoginGov).to receive(:new).and_return(login_gov)
allow(login_gov).to receive(:exchange_token_from_auth_result).with(code).and_return(
[{ email:, sub: token }]
)

# rubocop:disable RSpec/AnyInstance
allow_any_instance_of(SessionsController).to(
receive(:send_user_jwt_to_phoenix).with(instance_of(String)).and_return(true)
)
# rubocop:enable RSpec/AnyInstance
mock_login_gov(user, code)

get "/auth/result", params: { code: }

Expand Down
24 changes: 13 additions & 11 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,30 @@
config.shared_context_metadata_behavior = :apply_to_host_groups
end

# rubocop:disable Metrics/AbcSize
def create_and_log_in_user(user_attrs = {})
user = create_user(user_attrs)
code = "ABC123"
login_gov = instance_double(LoginGov)
allow(LoginGov).to receive(:new).and_return(login_gov)
allow(login_gov).to receive(:exchange_token_from_auth_result).with(code).and_return(
[{ email: user.email, sub: user.token }]
)

# rubocop:disable Layout/LineLength, RSpec/AnyInstance
allow_any_instance_of(SessionsController).to receive(:send_user_jwt_to_phoenix).with(instance_of(String)).and_return(true)
# rubocop:enable Layout/LineLength, RSpec/AnyInstance
mock_login_gov(user, code)

get "/auth/result", params: { code: }
user
end
# rubocop:enable Metrics/AbcSize

def create_user(user_attrs = {})
email = "[email protected]"
token = SecureRandom.uuid
user_attrs = { email:, token: }.merge(user_attrs)
User.create!(user_attrs)
end

def mock_login_gov(user, code = "ABC123") # rubocop:disable Metrics/AbcSize
login_gov = instance_double(LoginGov)
allow(LoginGov).to receive(:new).and_return(login_gov)
allow(login_gov).to receive(:exchange_token_from_auth_result).with(code).and_return(
[{ email: user.email, sub: user.token }]
)

allow_any_instance_of(SessionsController).to( # rubocop:disable RSpec/AnyInstance
receive(:send_user_jwt_to_phoenix).with(instance_of(String)).and_return(true)
)
end

0 comments on commit 3c990e0

Please sign in to comment.