1+ import { faker } from "@faker-js/faker" ;
2+ import { magicLink } from "../../src/authentication" ;
3+ import { Config } from "../../src/configuration" ;
4+ import { User } from "../../src/types/user" ;
5+ import { EmailAddress } from "../../src/types" ;
6+ import { createEmailVerificationLink , decodeEmailVerificationLink } from "../../src/authentication/verify-email/email-verification-link" ;
7+ import * as E from 'fp-ts/Either' ;
8+ import pino from "pino" ;
9+ import { Request } from 'express' ;
10+ import { decodeMagicLinkFromQuery } from "../../src/authentication/login/magic-link" ;
11+ import { getRightOrFail } from "../helpers" ;
12+
13+ const reqWithQuery = ( link : string ) : Request => ( {
14+ query : Object . fromEntries ( new URL ( link ) . searchParams . entries ( ) )
15+ } as unknown as Request ) ;
16+
17+ describe ( 'magic links' , ( ) => {
18+ const conf = {
19+ TOKEN_SECRET : 'secret' ,
20+ PUBLIC_URL : 'https://members.makespace.example' ,
21+ } as Config ;
22+ const logger = pino ( { level : 'silent' } ) ;
23+
24+ describe ( 'generates a login and a verification link' , ( ) => {
25+ const user : User = {
26+ emailAddress : faker . internet . email ( ) as EmailAddress ,
27+ memberNumber : faker . number . int ( ) ,
28+ } ;
29+ let loginLink : string ;
30+ let verificationLink : string ;
31+ beforeEach ( ( ) => {
32+ loginLink = magicLink . create ( conf ) ( user ) ;
33+ verificationLink = createEmailVerificationLink ( conf ) ( user ) ;
34+ } ) ;
35+
36+ it ( 'login link does not decode as an email verification link' , ( ) => {
37+ const req = reqWithQuery ( loginLink ) ;
38+ expect ( E . isLeft ( decodeEmailVerificationLink ( logger , conf ) ( req ) ) ) . toBeTruthy ( ) ;
39+ } ) ;
40+
41+ it ( 'email verification link does not decode as a login link' , ( ) => {
42+ const req = reqWithQuery ( verificationLink ) ;
43+ expect ( E . isLeft ( decodeMagicLinkFromQuery ( logger , conf ) ( req . query ) ) ) . toBeTruthy ( ) ;
44+ } ) ;
45+
46+ it ( 'login link does decode as an login link' , ( ) => {
47+ const req = reqWithQuery ( loginLink ) ;
48+ expect ( getRightOrFail ( decodeMagicLinkFromQuery ( logger , conf ) ( req . query ) ) ) . toStrictEqual ( user ) ;
49+ } ) ;
50+
51+ it ( 'email verification link does decode as an email verification link' , ( ) => {
52+ const req = reqWithQuery ( verificationLink ) ;
53+ expect ( getRightOrFail ( decodeEmailVerificationLink ( logger , conf ) ( req ) ) ) . toStrictEqual ( user ) ;
54+ } ) ;
55+ } ) ;
56+
57+ } ) ;
0 commit comments