|
| 1 | +import { TestingModule } from '@nestjs/testing'; |
| 2 | +import { PrismaClient } from '@prisma/client'; |
| 3 | +import ava, { TestFn } from 'ava'; |
| 4 | + |
| 5 | +import { Config } from '../../base/config'; |
| 6 | +import { UserModel } from '../../models/user'; |
| 7 | +import { UserSessionModel } from '../../models/user-session'; |
| 8 | +import { createTestingModule, initTestingDB } from '../utils'; |
| 9 | + |
| 10 | +interface Context { |
| 11 | + module: TestingModule; |
| 12 | + user: UserModel; |
| 13 | + userSession: UserSessionModel; |
| 14 | + db: PrismaClient; |
| 15 | + config: Config; |
| 16 | +} |
| 17 | + |
| 18 | +const test = ava as TestFn<Context>; |
| 19 | + |
| 20 | +test.before(async t => { |
| 21 | + const module = await createTestingModule({ |
| 22 | + providers: [UserModel, UserSessionModel], |
| 23 | + }); |
| 24 | + |
| 25 | + t.context.user = module.get(UserModel); |
| 26 | + t.context.userSession = module.get(UserSessionModel); |
| 27 | + t.context.module = module; |
| 28 | + t.context.db = t.context.module.get(PrismaClient); |
| 29 | + t.context.config = t.context.module.get(Config); |
| 30 | +}); |
| 31 | + |
| 32 | +test.beforeEach(async t => { |
| 33 | + await initTestingDB(t.context.module.get(PrismaClient)); |
| 34 | +}); |
| 35 | + |
| 36 | +test.after(async t => { |
| 37 | + await t.context.module.close(); |
| 38 | +}); |
| 39 | + |
| 40 | +test('should create a new userSession', async t => { |
| 41 | + const user = await t.context.user.create({ |
| 42 | + |
| 43 | + }); |
| 44 | + const session = await t.context.db.session.create({ |
| 45 | + data: {}, |
| 46 | + }); |
| 47 | + const userSession = await t.context.userSession.createOrRefresh( |
| 48 | + session.id, |
| 49 | + user.id |
| 50 | + ); |
| 51 | + t.is(userSession.sessionId, session.id); |
| 52 | + t.is(userSession.userId, user.id); |
| 53 | + t.not(userSession.expiresAt, null); |
| 54 | +}); |
| 55 | + |
| 56 | +test('should refresh exists userSession', async t => { |
| 57 | + const user = await t.context.user.create({ |
| 58 | + |
| 59 | + }); |
| 60 | + const session = await t.context.db.session.create({ |
| 61 | + data: {}, |
| 62 | + }); |
| 63 | + const userSession = await t.context.userSession.createOrRefresh( |
| 64 | + session.id, |
| 65 | + user.id |
| 66 | + ); |
| 67 | + t.is(userSession.sessionId, session.id); |
| 68 | + t.is(userSession.userId, user.id); |
| 69 | + t.not(userSession.expiresAt, null); |
| 70 | + |
| 71 | + const existsUserSession = await t.context.userSession.createOrRefresh( |
| 72 | + session.id, |
| 73 | + user.id |
| 74 | + ); |
| 75 | + t.is(existsUserSession.sessionId, session.id); |
| 76 | + t.is(existsUserSession.userId, user.id); |
| 77 | + t.not(existsUserSession.expiresAt, null); |
| 78 | + t.is(existsUserSession.id, userSession.id); |
| 79 | + t.assert( |
| 80 | + existsUserSession.expiresAt!.getTime() > userSession.expiresAt!.getTime() |
| 81 | + ); |
| 82 | +}); |
| 83 | + |
| 84 | +test('should not refresh userSession when expires time not hit ttr', async t => { |
| 85 | + const user = await t.context.user.create({ |
| 86 | + |
| 87 | + }); |
| 88 | + const session = await t.context.db.session.create({ |
| 89 | + data: {}, |
| 90 | + }); |
| 91 | + const userSession = await t.context.userSession.createOrRefresh( |
| 92 | + session.id, |
| 93 | + user.id |
| 94 | + ); |
| 95 | + let newExpiresAt = await t.context.userSession.refreshIfNeeded(userSession); |
| 96 | + t.is(newExpiresAt, undefined); |
| 97 | + userSession.expiresAt = new Date( |
| 98 | + userSession.expiresAt!.getTime() - t.context.config.auth.session.ttr * 1000 |
| 99 | + ); |
| 100 | + newExpiresAt = await t.context.userSession.refreshIfNeeded(userSession); |
| 101 | + t.is(newExpiresAt, undefined); |
| 102 | +}); |
| 103 | + |
| 104 | +test('should not refresh userSession when expires time hit ttr', async t => { |
| 105 | + const user = await t.context.user.create({ |
| 106 | + |
| 107 | + }); |
| 108 | + const session = await t.context.db.session.create({ |
| 109 | + data: {}, |
| 110 | + }); |
| 111 | + const userSession = await t.context.userSession.createOrRefresh( |
| 112 | + session.id, |
| 113 | + user.id |
| 114 | + ); |
| 115 | + const ttr = t.context.config.auth.session.ttr * 2; |
| 116 | + userSession.expiresAt = new Date( |
| 117 | + userSession.expiresAt!.getTime() - ttr * 1000 |
| 118 | + ); |
| 119 | + const newExpiresAt = await t.context.userSession.refreshIfNeeded(userSession); |
| 120 | + t.not(newExpiresAt, undefined); |
| 121 | +}); |
| 122 | + |
| 123 | +test('should find userSessions without user property by default', async t => { |
| 124 | + const session = await t.context.db.session.create({ |
| 125 | + data: {}, |
| 126 | + }); |
| 127 | + const count = 10; |
| 128 | + for (let i = 0; i < count; i++) { |
| 129 | + const user = await t.context.user.create({ |
| 130 | + email: `test${i}@affine.pro`, |
| 131 | + }); |
| 132 | + await t.context.userSession.createOrRefresh(session.id, user.id); |
| 133 | + } |
| 134 | + const userSessions = await t.context.userSession.findManyBySessionId( |
| 135 | + session.id |
| 136 | + ); |
| 137 | + t.is(userSessions.length, count); |
| 138 | + for (const userSession of userSessions) { |
| 139 | + t.is(userSession.sessionId, session.id); |
| 140 | + t.is(userSession.user, undefined); |
| 141 | + } |
| 142 | +}); |
| 143 | + |
| 144 | +test('should find userSessions include user property', async t => { |
| 145 | + const session = await t.context.db.session.create({ |
| 146 | + data: {}, |
| 147 | + }); |
| 148 | + const count = 10; |
| 149 | + for (let i = 0; i < count; i++) { |
| 150 | + const user = await t.context.user.create({ |
| 151 | + email: `test${i}@affine.pro`, |
| 152 | + }); |
| 153 | + await t.context.userSession.createOrRefresh(session.id, user.id); |
| 154 | + } |
| 155 | + const userSessions = await t.context.userSession.findManyBySessionId( |
| 156 | + session.id, |
| 157 | + { user: true } |
| 158 | + ); |
| 159 | + t.is(userSessions.length, count); |
| 160 | + for (const userSession of userSessions) { |
| 161 | + t.is(userSession.sessionId, session.id); |
| 162 | + t.not(userSession.user, undefined); |
| 163 | + } |
| 164 | +}); |
| 165 | + |
| 166 | +test('should delete userSession success by userId', async t => { |
| 167 | + const user = await t.context.user.create({ |
| 168 | + |
| 169 | + }); |
| 170 | + const session = await t.context.db.session.create({ |
| 171 | + data: {}, |
| 172 | + }); |
| 173 | + await t.context.userSession.createOrRefresh(session.id, user.id); |
| 174 | + let count = await t.context.userSession.delete(user.id); |
| 175 | + t.is(count, 1); |
| 176 | + count = await t.context.userSession.delete(user.id); |
| 177 | + t.is(count, 0); |
| 178 | +}); |
| 179 | + |
| 180 | +test('should delete userSession success by userId and sessionId', async t => { |
| 181 | + const user = await t.context.user.create({ |
| 182 | + |
| 183 | + }); |
| 184 | + const session = await t.context.db.session.create({ |
| 185 | + data: {}, |
| 186 | + }); |
| 187 | + await t.context.userSession.createOrRefresh(session.id, user.id); |
| 188 | + const count = await t.context.userSession.delete(user.id, session.id); |
| 189 | + t.is(count, 1); |
| 190 | +}); |
| 191 | + |
| 192 | +test('should delete userSession fail when sessionId not match', async t => { |
| 193 | + const user = await t.context.user.create({ |
| 194 | + |
| 195 | + }); |
| 196 | + const session = await t.context.db.session.create({ |
| 197 | + data: {}, |
| 198 | + }); |
| 199 | + await t.context.userSession.createOrRefresh(session.id, user.id); |
| 200 | + const count = await t.context.userSession.delete( |
| 201 | + user.id, |
| 202 | + 'not-exists-session-id' |
| 203 | + ); |
| 204 | + t.is(count, 0); |
| 205 | +}); |
| 206 | + |
| 207 | +test('should cleanup expired userSessions', async t => { |
| 208 | + const user = await t.context.user.create({ |
| 209 | + |
| 210 | + }); |
| 211 | + const session = await t.context.db.session.create({ |
| 212 | + data: {}, |
| 213 | + }); |
| 214 | + const userSession = await t.context.userSession.createOrRefresh( |
| 215 | + session.id, |
| 216 | + user.id |
| 217 | + ); |
| 218 | + await t.context.userSession.cleanExpiredUserSessions(); |
| 219 | + let count = await t.context.db.userSession.count(); |
| 220 | + t.is(count, 1); |
| 221 | + |
| 222 | + // Set expiresAt to past time |
| 223 | + await t.context.db.userSession.update({ |
| 224 | + where: { id: userSession.id }, |
| 225 | + data: { expiresAt: new Date('2022-01-01') }, |
| 226 | + }); |
| 227 | + await t.context.userSession.cleanExpiredUserSessions(); |
| 228 | + count = await t.context.db.userSession.count(); |
| 229 | + t.is(count, 0); |
| 230 | +}); |
0 commit comments