|
| 1 | +import { getAgent, getLoggedInAgent, getServer } from '@/fixtures'; |
| 2 | +import User from '@/models/user'; |
| 3 | + |
| 4 | +describe('me router', () => { |
| 5 | + const server = getServer(); |
| 6 | + |
| 7 | + beforeAll(async () => { |
| 8 | + await server.start(); |
| 9 | + }); |
| 10 | + |
| 11 | + afterEach(async () => { |
| 12 | + await server.clearDBs(); |
| 13 | + }); |
| 14 | + |
| 15 | + afterAll(async () => { |
| 16 | + await server.stop(); |
| 17 | + }); |
| 18 | + |
| 19 | + describe('GET /me', () => { |
| 20 | + it('returns the calling user', async () => { |
| 21 | + const { agent, team, user } = await getLoggedInAgent(server); |
| 22 | + |
| 23 | + const resp = await agent.get('/me').expect(200); |
| 24 | + |
| 25 | + expect(resp.body.id).toEqual(user._id.toString()); |
| 26 | + expect(resp.body.email).toEqual('fake@deploysentinel.com'); |
| 27 | + expect(resp.body.accessKey).toEqual(user.accessKey); |
| 28 | + expect(resp.body.team.id).toEqual(team._id.toString()); |
| 29 | + }); |
| 30 | + |
| 31 | + it('rejects an unauthenticated request', async () => { |
| 32 | + await getAgent(server).get('/me').expect(401); |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + describe('PATCH /me/accessKey', () => { |
| 37 | + it('rejects an unauthenticated request', async () => { |
| 38 | + // The new verb is covered by the mount-time isUserAuthenticated in |
| 39 | + // api-app.ts, not by anything in the handler itself. |
| 40 | + await getAgent(server).patch('/me/accessKey').expect(401); |
| 41 | + }); |
| 42 | + |
| 43 | + it('returns a new key and persists it', async () => { |
| 44 | + const { agent, user } = await getLoggedInAgent(server); |
| 45 | + |
| 46 | + const resp = await agent.patch('/me/accessKey').expect(200); |
| 47 | + |
| 48 | + expect(resp.body.newAccessKey).toEqual(expect.any(String)); |
| 49 | + expect(resp.body.newAccessKey).not.toEqual(user.accessKey); |
| 50 | + expect((await User.findById(user._id))?.accessKey).toEqual( |
| 51 | + resp.body.newAccessKey, |
| 52 | + ); |
| 53 | + }); |
| 54 | + |
| 55 | + it('revokes the old key and accepts the new one', async () => { |
| 56 | + const { agent, user } = await getLoggedInAgent(server); |
| 57 | + const oldAccessKey = user.accessKey; |
| 58 | + |
| 59 | + // GET /api/v2 is the bearer-authed surface with no rate limiter attached, |
| 60 | + // so three sequential calls here are safe. |
| 61 | + await agent |
| 62 | + .get('/api/v2') |
| 63 | + .set('Authorization', `Bearer ${oldAccessKey}`) |
| 64 | + .expect(200); |
| 65 | + |
| 66 | + const { body } = await agent.patch('/me/accessKey').expect(200); |
| 67 | + |
| 68 | + await agent |
| 69 | + .get('/api/v2') |
| 70 | + .set('Authorization', `Bearer ${oldAccessKey}`) |
| 71 | + .expect(401); |
| 72 | + await agent |
| 73 | + .get('/api/v2') |
| 74 | + .set('Authorization', `Bearer ${body.newAccessKey}`) |
| 75 | + .expect(200); |
| 76 | + }); |
| 77 | + |
| 78 | + it('does not sign the user out of their browser session', async () => { |
| 79 | + // Session auth never reads accessKey (see isUserAuthenticated), so |
| 80 | + // rotating must leave the cookie session intact. |
| 81 | + const { agent } = await getLoggedInAgent(server); |
| 82 | + |
| 83 | + const { body } = await agent.patch('/me/accessKey').expect(200); |
| 84 | + |
| 85 | + const resp = await agent.get('/me').expect(200); |
| 86 | + expect(resp.body.accessKey).toEqual(body.newAccessKey); |
| 87 | + }); |
| 88 | + |
| 89 | + it("does not touch another user's key", async () => { |
| 90 | + const { agent, user } = await getLoggedInAgent(server); |
| 91 | + // Created directly rather than via /register/password, which is gated to |
| 92 | + // the first user. We only read the schema-defaulted accessKey off it. |
| 93 | + const other = await User.create({ |
| 94 | + email: 'other@deploysentinel.com', |
| 95 | + team: user.team, |
| 96 | + }); |
| 97 | + |
| 98 | + await agent.patch('/me/accessKey').expect(200); |
| 99 | + |
| 100 | + expect((await User.findById(other._id))?.accessKey).toEqual( |
| 101 | + other.accessKey, |
| 102 | + ); |
| 103 | + }); |
| 104 | + }); |
| 105 | +}); |
0 commit comments