@@ -6,6 +6,10 @@ import { ThemeUIProvider } from 'theme-ui'
66import App from './app'
77import rootReducer from './reducers'
88import theme from './theme'
9+ import { reloadPage , replacePage } from './util/page-navigation'
10+
11+ jest . setTimeout ( 30000 )
12+ const PATIENT = { timeout : 15000 }
913
1014const mockInit = jest . fn ( )
1115const mockRestore = jest . fn ( )
@@ -33,6 +37,11 @@ jest.mock('@atproto/api', () => ({
3337 }
3438} ) )
3539
40+ jest . mock ( './util/page-navigation' , ( ) => ( {
41+ reloadPage : jest . fn ( ) ,
42+ replacePage : jest . fn ( )
43+ } ) )
44+
3645jest . mock ( './components/conversations-and-account/conversations' , ( ) => {
3746 return function MockConversations ( ) {
3847 return < div > All Conversations</ div >
@@ -50,7 +59,7 @@ const PROOF_REQUEST = {
5059const PROOF_MESSAGE =
5160 'We could not confirm this account with its host. Sign in again. If this keeps happening, your host may not support this sign-in yet.'
5261const UNSUPPORTED_DID_MESSAGE =
53- 'Accounts with a did:web identifier cannot sign in to the admin console yet.'
62+ 'This kind of account identifier cannot sign in to the admin console yet.'
5463const LOOKUP_MESSAGE = 'We could not look up your account just now. Try again in a minute.'
5564
5665const did = 'did:plc:abcdefghijklmnopqrstuvwx'
@@ -126,40 +135,48 @@ describe('admin console sign-in', () => {
126135
127136 it ( 'restores the OAuth session of that identity and signs in with proof' , async ( ) => {
128137 mockRestore . mockResolvedValue ( session )
129- mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof-1 ' } } )
138+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature ' } } )
130139 window . fetch . mockResolvedValue ( serverAnswers ( 200 , { token : 'admin-1' , uid : 7 } ) )
131140
132141 openConsole ( '/' )
133142
134- await waitFor ( ( ) => expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBe ( 'admin-1' ) )
143+ await waitFor ( ( ) => expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBe ( 'admin-1' ) , PATIENT )
135144 expect ( mockRestore . mock . calls ) . toEqual ( [ [ did ] ] )
136145 expect ( mockAgent . mock . calls ) . toEqual ( [ [ session ] ] )
137146 expect ( mockGetServiceAuth . mock . calls ) . toEqual ( [ [ PROOF_REQUEST ] ] )
138147 expect ( loginRequests ( ) ) . toEqual ( [
139148 {
140- headers : { 'Content-Type' : 'application/json' , Authorization : 'Bearer proof-1' } ,
149+ headers : {
150+ 'Content-Type' : 'application/json' ,
151+ Authorization : 'Bearer proof.number1.signature'
152+ } ,
141153 body : storedIdentity
142154 }
143155 ] )
144156 expect ( JSON . parse ( localStorage . getItem ( IDENTITY_KEY ) ) ) . toEqual ( storedIdentity )
157+ await waitFor ( ( ) => expect ( reloadPage ) . toHaveBeenCalledTimes ( 1 ) , PATIENT )
158+ expect ( replacePage ) . toHaveBeenCalledTimes ( 0 )
145159 } )
146160
147161 it ( 'ends signed out on the sign-in page with the reason when the server refuses the proof' , async ( ) => {
148162 mockRestore . mockResolvedValue ( session )
149- mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof-1 ' } } )
163+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature ' } } )
150164 window . fetch . mockResolvedValue (
151165 serverAnswers ( 401 , { error : 'polis_err_atproto_auth_invalid' , status : 401 } )
152166 )
153167
154168 openConsole ( '/' )
155169
156- expect ( await screen . findAllByText ( PROOF_MESSAGE ) ) . toHaveLength ( 1 )
170+ expect ( await screen . findAllByText ( PROOF_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
157171 expect ( window . location . pathname ) . toBe ( '/signin' )
158172 expect ( screen . getByRole ( 'button' , { name : 'Sign In' } ) ) . toBeInTheDocument ( )
159173 expect ( screen . queryByText ( 'All Conversations' ) ) . toBeNull ( )
160174 expect ( loginRequests ( ) ) . toEqual ( [
161175 {
162- headers : { 'Content-Type' : 'application/json' , Authorization : 'Bearer proof-1' } ,
176+ headers : {
177+ 'Content-Type' : 'application/json' ,
178+ Authorization : 'Bearer proof.number1.signature'
179+ } ,
163180 body : storedIdentity
164181 }
165182 ] )
@@ -175,7 +192,7 @@ describe('admin console sign-in', () => {
175192
176193 openConsole ( '/' )
177194
178- expect ( await screen . findAllByText ( PROOF_MESSAGE ) ) . toHaveLength ( 1 )
195+ expect ( await screen . findAllByText ( PROOF_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
179196 expect ( window . location . pathname ) . toBe ( '/signin' )
180197 expect ( mockRestore . mock . calls ) . toEqual ( [ [ did ] ] )
181198 expect ( mockGetServiceAuth ) . toHaveBeenCalledTimes ( 0 )
@@ -197,7 +214,7 @@ describe('admin console sign-in', () => {
197214
198215 openConsole ( '/' )
199216
200- expect ( await screen . findAllByText ( PROOF_MESSAGE ) ) . toHaveLength ( 1 )
217+ expect ( await screen . findAllByText ( PROOF_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
201218 expect ( mockGetServiceAuth . mock . calls ) . toEqual ( [ [ PROOF_REQUEST ] ] )
202219 expect ( loginRequests ( ) ) . toEqual ( [
203220 { headers : { 'Content-Type' : 'application/json' } , body : storedIdentity }
@@ -208,14 +225,14 @@ describe('admin console sign-in', () => {
208225
209226 it ( 'shows the lookup message when the server cannot look the account up' , async ( ) => {
210227 mockRestore . mockResolvedValue ( session )
211- mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof-1 ' } } )
228+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature ' } } )
212229 window . fetch . mockResolvedValue (
213230 serverAnswers ( 503 , { error : 'polis_err_atproto_did_resolution_failed' , status : 503 } )
214231 )
215232
216233 openConsole ( '/' )
217234
218- expect ( await screen . findAllByText ( LOOKUP_MESSAGE ) ) . toHaveLength ( 1 )
235+ expect ( await screen . findAllByText ( LOOKUP_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
219236 expect ( window . location . pathname ) . toBe ( '/signin' )
220237 expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBeNull ( )
221238 expect ( JSON . parse ( localStorage . getItem ( IDENTITY_KEY ) ) ) . toEqual ( storedIdentity )
@@ -225,39 +242,62 @@ describe('admin console sign-in', () => {
225242 describe ( 'returning from the authorization server' , ( ) => {
226243 it ( 'signs in with proof from the new session' , async ( ) => {
227244 mockInit . mockResolvedValue ( { session } )
228- mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof-1 ' } } )
245+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature ' } } )
229246 window . fetch . mockResolvedValue ( serverAnswers ( 200 , { token : 'admin-1' , uid : 7 } ) )
230247
231248 openConsole ( '/?state=s1&code=c1' )
232249
233- await waitFor ( ( ) => expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBe ( 'admin-1' ) )
250+ await waitFor ( ( ) => expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBe ( 'admin-1' ) , PATIENT )
234251 expect ( mockInit ) . toHaveBeenCalledTimes ( 1 )
235252 expect ( mockRestore ) . toHaveBeenCalledTimes ( 0 )
236253 expect ( mockAgent . mock . calls ) . toEqual ( [ [ session ] ] )
237254 expect ( mockGetServiceAuth . mock . calls ) . toEqual ( [ [ PROOF_REQUEST ] ] )
238255 expect ( loginRequests ( ) ) . toEqual ( [
239256 {
240- headers : { 'Content-Type' : 'application/json' , Authorization : 'Bearer proof-1' } ,
257+ headers : {
258+ 'Content-Type' : 'application/json' ,
259+ Authorization : 'Bearer proof.number1.signature'
260+ } ,
241261 body : { ...freshIdentity , email }
242262 }
243263 ] )
244264 expect ( JSON . parse ( localStorage . getItem ( IDENTITY_KEY ) ) ) . toEqual ( freshIdentity )
265+ await waitFor ( ( ) => expect ( replacePage . mock . calls ) . toEqual ( [ [ '/' ] ] ) , PATIENT )
266+ expect ( reloadPage ) . toHaveBeenCalledTimes ( 0 )
267+ } )
268+
269+ it ( 'shows the reason on the sign-in page when the return address is the callback route' , async ( ) => {
270+ mockInit . mockResolvedValueOnce ( { session } ) . mockResolvedValue ( null )
271+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature' } } )
272+ window . fetch . mockResolvedValue (
273+ serverAnswers ( 401 , { error : 'polis_err_atproto_auth_invalid' , status : 401 } )
274+ )
275+
276+ openConsole ( '/auth/callback?state=s1&code=c1' )
277+
278+ expect ( await screen . findAllByText ( PROOF_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
279+ expect ( window . location . pathname ) . toBe ( '/signin' )
280+ expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBeNull ( )
281+ expect ( replacePage ) . toHaveBeenCalledTimes ( 0 )
245282 } )
246283
247284 it ( 'ends on the sign-in page with the reason and stores nothing when the server refuses' , async ( ) => {
248285 mockInit . mockResolvedValue ( { session } )
249- mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof-1 ' } } )
286+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature ' } } )
250287 window . fetch . mockResolvedValue (
251288 serverAnswers ( 400 , { error : 'polis_err_atproto_unsupported_did' , status : 400 } )
252289 )
253290
254291 openConsole ( '/?state=s1&code=c1' )
255292
256- expect ( await screen . findAllByText ( UNSUPPORTED_DID_MESSAGE ) ) . toHaveLength ( 1 )
293+ expect ( await screen . findAllByText ( UNSUPPORTED_DID_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
257294 expect ( window . location . pathname ) . toBe ( '/signin' )
258295 expect ( loginRequests ( ) ) . toEqual ( [
259296 {
260- headers : { 'Content-Type' : 'application/json' , Authorization : 'Bearer proof-1' } ,
297+ headers : {
298+ 'Content-Type' : 'application/json' ,
299+ Authorization : 'Bearer proof.number1.signature'
300+ } ,
261301 body : { ...freshIdentity , email }
262302 }
263303 ] )
@@ -267,14 +307,14 @@ describe('admin console sign-in', () => {
267307 it ( 'does not remove an identity stored earlier when the server refuses' , async ( ) => {
268308 localStorage . setItem ( IDENTITY_KEY , JSON . stringify ( storedIdentity ) )
269309 mockInit . mockResolvedValue ( { session } )
270- mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof-1 ' } } )
310+ mockGetServiceAuth . mockResolvedValue ( { data : { token : 'proof.number1.signature ' } } )
271311 window . fetch . mockResolvedValue (
272312 serverAnswers ( 401 , { error : 'polis_err_atproto_auth_replayed' , status : 401 } )
273313 )
274314
275315 openConsole ( '/?state=s1&code=c1' )
276316
277- expect ( await screen . findAllByText ( PROOF_MESSAGE ) ) . toHaveLength ( 1 )
317+ expect ( await screen . findAllByText ( PROOF_MESSAGE , { } , PATIENT ) ) . toHaveLength ( 1 )
278318 expect ( localStorage . getItem ( ADMIN_TOKEN_KEY ) ) . toBeNull ( )
279319 expect ( JSON . parse ( localStorage . getItem ( IDENTITY_KEY ) ) ) . toEqual ( storedIdentity )
280320 } )
0 commit comments