feat(server): authenticate user before ws connected (#7777)

This commit is contained in:
forehalo
2024-08-08 08:30:55 +00:00
parent 83244f0201
commit f2eafc374c
17 changed files with 232 additions and 87 deletions
@@ -69,7 +69,7 @@ test('should be able to visit public api if signed in', async t => {
const { app, auth } = t.context;
// @ts-expect-error mock
auth.getUser.resolves({ user: { id: '1' } });
auth.getUserSession.resolves({ user: { id: '1' }, session: { id: '1' } });
const res = await request(app.getHttpServer())
.get('/public')
@@ -100,7 +100,7 @@ test('should be able to visit private api if signed in', async t => {
const { app, auth } = t.context;
// @ts-expect-error mock
auth.getUser.resolves({ user: { id: '1' } });
auth.getUserSession.resolves({ user: { id: '1' }, session: { id: '1' } });
const res = await request(app.getHttpServer())
.get('/private')
@@ -114,26 +114,26 @@ test('should be able to parse session cookie', async t => {
const { app, auth } = t.context;
// @ts-expect-error mock
auth.getUser.resolves({ user: { id: '1' } });
auth.getUserSession.resolves({ user: { id: '1' }, session: { id: '1' } });
await request(app.getHttpServer())
.get('/public')
.set('cookie', `${AuthService.sessionCookieName}=1`)
.expect(200);
t.deepEqual(auth.getUser.firstCall.args, ['1', 0]);
t.deepEqual(auth.getUserSession.firstCall.args, ['1', 0]);
});
test('should be able to parse bearer token', async t => {
const { app, auth } = t.context;
// @ts-expect-error mock
auth.getUser.resolves({ user: { id: '1' } });
auth.getUserSession.resolves({ user: { id: '1' }, session: { id: '1' } });
await request(app.getHttpServer())
.get('/public')
.auth('1', { type: 'bearer' })
.expect(200);
t.deepEqual(auth.getUser.firstCall.args, ['1', 0]);
t.deepEqual(auth.getUserSession.firstCall.args, ['1', 0]);
});
@@ -157,10 +157,10 @@ test('should be able to get user from session', async t => {
const session = await auth.createUserSession(u1);
const { user } = await auth.getUser(session.sessionId);
const userSession = await auth.getUserSession(session.sessionId);
t.not(user, null);
t.is(user!.id, u1.id);
t.not(userSession, null);
t.is(userSession!.user.id, u1.id);
});
test('should be able to sign out session', async t => {
@@ -203,19 +203,19 @@ test('should be able to signout multi accounts session', async t => {
t.not(signedOutSession, null);
const { user: signedU2 } = await auth.getUser(session.sessionId, 0);
const { user: noUser } = await auth.getUser(session.sessionId, 1);
const userSession1 = await auth.getUserSession(session.sessionId, 0);
const userSession2 = await auth.getUserSession(session.sessionId, 1);
t.is(noUser, null);
t.not(signedU2, null);
t.is(userSession2, null);
t.not(userSession1, null);
t.is(signedU2!.id, u2.id);
t.is(userSession1!.user.id, u2.id);
// sign out user at seq(0)
signedOutSession = await auth.signOut(session.sessionId);
t.is(signedOutSession, null);
const { user: noUser2 } = await auth.getUser(session.sessionId, 0);
t.is(noUser2, null);
const userSession3 = await auth.getUserSession(session.sessionId, 0);
t.is(userSession3, null);
});