|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | describe WorkOS::Session do |
4 | | - let(:user_management) { instance_double('UserManagement') } |
5 | 4 | let(:client_id) { 'test_client_id' } |
6 | 5 | let(:cookie_password) { 'test_very_long_cookie_password__' } |
7 | 6 | let(:session_data) { 'test_session_data' } |
|
10 | 9 | let(:jwk) { JWT::JWK.new(OpenSSL::PKey::RSA.new(2048), { kid: 'sso_oidc_key_pair_123', use: 'sig', alg: 'RS256' }) } |
11 | 10 |
|
12 | 11 | before do |
13 | | - allow(user_management).to receive(:get_jwks_url).with(client_id).and_return(jwks_url) |
14 | 12 | allow(Net::HTTP).to receive(:get).and_return(jwks_hash) |
15 | 13 | end |
16 | 14 |
|
17 | 15 | describe 'initialize' do |
| 16 | + let(:user_management) { instance_double('UserManagement') } |
| 17 | + |
| 18 | + before do |
| 19 | + allow(user_management).to receive(:get_jwks_url).with(client_id).and_return(jwks_url) |
| 20 | + end |
| 21 | + |
18 | 22 | it 'raises an error if cookie_password is nil or empty' do |
19 | 23 | expect do |
20 | 24 | WorkOS::Session.new( |
|
52 | 56 | end |
53 | 57 |
|
54 | 58 | describe '.authenticate' do |
| 59 | + let(:user_management) { instance_double('UserManagement') } |
55 | 60 | let(:valid_access_token) do |
56 | 61 | payload = { |
57 | 62 | sid: 'session_id', |
|
71 | 76 | }, cookie_password,) |
72 | 77 | end |
73 | 78 |
|
| 79 | + before do |
| 80 | + allow(user_management).to receive(:get_jwks_url).with(client_id).and_return(jwks_url) |
| 81 | + end |
| 82 | + |
74 | 83 | it 'returns NO_SESSION_COOKIE_PROVIDED if session_data is nil' do |
75 | 84 | session = WorkOS::Session.new( |
76 | 85 | user_management: user_management, |
|
135 | 144 | end |
136 | 145 |
|
137 | 146 | describe '.refresh' do |
| 147 | + let(:user_management) { instance_double('UserManagement') } |
138 | 148 | let(:refresh_token) { 'test_refresh_token' } |
139 | 149 | let(:session_data) { WorkOS::Session.seal_data({ refresh_token: refresh_token, user: 'user' }, cookie_password) } |
140 | 150 | let(:auth_response) { double('AuthResponse', sealed_session: 'new_sealed_session') } |
141 | 151 |
|
142 | 152 | before do |
| 153 | + allow(user_management).to receive(:get_jwks_url).with(client_id).and_return(jwks_url) |
143 | 154 | allow(user_management).to receive(:authenticate_with_refresh_token).and_return(auth_response) |
144 | 155 | end |
145 | 156 |
|
|
173 | 184 |
|
174 | 185 | describe '.get_logout_url' do |
175 | 186 | let(:session) do |
176 | | - WorkOS::Session.new( |
177 | | - user_management: user_management, |
178 | | - client_id: client_id, |
179 | | - session_data: session_data, |
180 | | - cookie_password: cookie_password, |
181 | | - ) |
182 | | - end |
| 187 | + WorkOS::Session.new( |
| 188 | + user_management: WorkOS::UserManagement, |
| 189 | + client_id: client_id, |
| 190 | + session_data: session_data, |
| 191 | + cookie_password: cookie_password, |
| 192 | + ) |
| 193 | + end |
183 | 194 |
|
184 | 195 | context 'when authentication is successful' do |
185 | 196 | before do |
186 | 197 | allow(session).to receive(:authenticate).and_return({ |
187 | 198 | authenticated: true, |
188 | | - session_id: 'session_id', |
| 199 | + session_id: 'session_123abc', |
189 | 200 | reason: nil, |
190 | 201 | }) |
191 | | - allow(user_management).to receive(:get_logout_url).with(session_id: 'session_id').and_return('https://example.com/logout') |
192 | 202 | end |
193 | 203 |
|
194 | 204 | it 'returns the logout URL' do |
195 | | - expect(session.get_logout_url).to eq('https://example.com/logout') |
| 205 | + expect(session.get_logout_url).to eq('https://api.workos.com/user_management/sessions/logout?session_id=session_123abc') |
| 206 | + end |
| 207 | + |
| 208 | + context 'when given a return_to URL' do |
| 209 | + it 'returns the logout URL with the return_to parameter' do |
| 210 | + expect(session.get_logout_url(return_to: 'https://example.com/signed-out')).to eq( |
| 211 | + 'https://api.workos.com/user_management/sessions/logout?session_id=session_123abc&return_to=https%3A%2F%2Fexample.com%2Fsigned-out', |
| 212 | + ) |
| 213 | + end |
196 | 214 | end |
197 | 215 | end |
198 | 216 |
|
|
0 commit comments