|
1 | | -require 'test_helper' |
| 1 | +# frozen_string_literal: true |
| 2 | +# rubocop:disable Metrics/BlockLength |
| 3 | +require "test_helper" |
2 | 4 |
|
3 | 5 | describe UserConProfilesController do |
4 | 6 | let(:user_con_profile) { create(:user_con_profile) } |
|
10 | 12 | profile |
11 | 13 | end |
12 | 14 | let(:con_admin) { con_admin_profile.user } |
| 15 | + let(:frontend_app) { create(:oauth_application, is_intercode_frontend: true) } |
13 | 16 |
|
14 | 17 | setup do |
15 | 18 | set_convention convention |
16 | 19 | sign_in con_admin |
17 | | - |
18 | 20 | user_con_profile |
| 21 | + frontend_app |
19 | 22 | end |
20 | 23 |
|
21 | | - # TODO write tests for become/revert_become |
| 24 | + describe "POST become" do |
| 25 | + it "creates an assumed identity session and issues an OAuth session for the assumed user" do |
| 26 | + OAuthApplication.stub(:find_by, frontend_app) do |
| 27 | + assert_difference("AssumedIdentitySession.count", 1) do |
| 28 | + assert_difference("Doorkeeper::AccessToken.count", 1) do |
| 29 | + post :become, params: { id: user_con_profile.id, justification: "testing become" } |
| 30 | + end |
| 31 | + end |
| 32 | + end |
| 33 | + |
| 34 | + assert_redirected_to root_url |
| 35 | + |
| 36 | + new_token = Doorkeeper::AccessToken.last |
| 37 | + assert_equal user_con_profile.user.id, new_token.resource_owner_id |
| 38 | + assert_equal frontend_app.id, new_token.application_id |
| 39 | + end |
| 40 | + |
| 41 | + it "revokes the admin's previous OAuth session cookie when one exists" do |
| 42 | + admin_token = |
| 43 | + Doorkeeper::AccessToken.create!( |
| 44 | + application: frontend_app, |
| 45 | + resource_owner_id: con_admin.id, |
| 46 | + scopes: "public", |
| 47 | + expires_in: 2.weeks, |
| 48 | + use_refresh_token: true |
| 49 | + ) |
| 50 | + @request.cookies[OAuthSessionManagement::REFRESH_COOKIE_NAME] = admin_token.plaintext_refresh_token |
| 51 | + |
| 52 | + OAuthApplication.stub(:find_by, frontend_app) do |
| 53 | + post :become, params: { id: user_con_profile.id, justification: "testing cookie revocation" } |
| 54 | + end |
| 55 | + |
| 56 | + assert admin_token.reload.revoked?, "Expected the admin's previous OAuth token to be revoked" |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + describe "POST revert_become" do |
| 61 | + setup do |
| 62 | + OAuthApplication.stub(:find_by, frontend_app) do |
| 63 | + post :become, params: { id: user_con_profile.id, justification: "setup for revert test" } |
| 64 | + end |
| 65 | + end |
| 66 | + |
| 67 | + it "reverts to the original admin user and issues a new OAuth session for them" do |
| 68 | + assumed_user_token = Doorkeeper::AccessToken.last |
| 69 | + |
| 70 | + OAuthApplication.stub(:find_by, frontend_app) do |
| 71 | + assert_difference("Doorkeeper::AccessToken.count", 1) { post :revert_become } |
| 72 | + end |
| 73 | + |
| 74 | + assert_redirected_to root_url |
| 75 | + |
| 76 | + new_token = Doorkeeper::AccessToken.last |
| 77 | + assert_equal con_admin.id, new_token.resource_owner_id |
| 78 | + assert assumed_user_token.reload.revoked?, "Expected the assumed user's OAuth token to be revoked after revert" |
| 79 | + end |
| 80 | + end |
22 | 81 | end |
| 82 | +# rubocop:enable Metrics/BlockLength |
0 commit comments