@@ -3,20 +3,73 @@ import { assertEquals } from "https://deno.land/std@0.208.0/assert/mod.ts";
33import { faker } from "https://deno.land/x/deno_faker@v1.0.3/mod.ts" ;
44
55test ( "e2e" , async ( ctx : TestContext ) => {
6- const authRes = await ctx . modules . auth . authEmailPasswordless ( {
7- email : faker . internet . email ( ) ,
8- } ) ;
6+ // First we create a new user, and "register" into the auth
7+ // using an authEmailPasswordless({ email, userToken })
8+ // call
9+ const { user } = await ctx . modules . users . createUser ( { } ) ;
910
10- // Look up correct code
11- const { code } = await ctx . db . emailPasswordlessVerification . findFirstOrThrow ( {
12- where : {
13- id : authRes . verification . id ,
14- } ,
11+ const { token : session } = await ctx . modules . users . createUserToken ( {
12+ userId : user . id
1513 } ) ;
1614
17- const verifyRes = await ctx . modules . auth . verifyEmailPasswordless ( {
18- verificationId : authRes . verification . id ,
19- code : code ,
20- } ) ;
21- assertEquals ( verifyRes . token . type , "user" ) ;
15+ const fakeEmail = faker . internet . email ( ) ;
16+
17+ // Now we test that post-signin, we get the same user
18+ {
19+ const authRes = await ctx . modules . auth . authEmailPasswordless ( {
20+ email : fakeEmail ,
21+ userToken : session . token
22+ } ) ;
23+
24+ // Look up correct code
25+ const { code } = await ctx . db . emailPasswordlessVerification . findFirstOrThrow ( {
26+ where : {
27+ id : authRes . verification . id ,
28+ } ,
29+ } ) ;
30+
31+ // Now by verifying the email, we register, and can also use
32+ // this to verify the token
33+ const verifyRes = await ctx . modules . auth . verifyEmailPasswordless ( {
34+ verificationId : authRes . verification . id ,
35+ code : code ,
36+ } ) ;
37+
38+ assertEquals ( verifyRes . token . type , "user" ) ;
39+
40+
41+ // Make sure we end up with the same user we started with
42+ const verifyRes2 = await ctx . modules . users . authenticateUser ( {
43+ userToken : verifyRes . token . token
44+ } ) ;
45+
46+ assertEquals ( verifyRes2 . userId , user . id ) ;
47+ }
48+
49+ // Now we try logging back in with the same email,
50+ // but without a token, expecting the same user
51+ {
52+ const authRes = await ctx . modules . auth . authEmailPasswordless ( {
53+ email : fakeEmail
54+ } ) ;
55+
56+ // Look up correct code
57+ const { code : code } = await ctx . db . emailPasswordlessVerification . findFirstOrThrow ( {
58+ where : {
59+ id : authRes . verification . id ,
60+ } ,
61+ } ) ;
62+
63+ const verifyRes = await ctx . modules . auth . verifyEmailPasswordless ( {
64+ verificationId : authRes . verification . id ,
65+ code : code ,
66+ } ) ;
67+
68+ const verifyRes2 = await ctx . modules . users . authenticateUser ( {
69+ userToken : verifyRes . token . token
70+ } ) ;
71+
72+ assertEquals ( verifyRes2 . userId , user . id ) ;
73+ }
2274} ) ;
75+
0 commit comments