@@ -2,7 +2,7 @@ import { expect, test, vi, beforeEach, describe } from 'vitest';
22import { Session } from 'next-auth' ;
33import { notAuthenticated } from './lib/serviceError' ;
44import { getAuthContext , getAuthenticatedUser , withAuthV2 , withOptionalAuthV2 } from './withAuthV2' ;
5- import { MOCK_API_KEY , MOCK_ORG , MOCK_USER_WITH_ACCOUNTS , prisma } from './__mocks__/prisma' ;
5+ import { MOCK_API_KEY , MOCK_OAUTH_TOKEN , MOCK_ORG , MOCK_USER_WITH_ACCOUNTS , prisma } from './__mocks__/prisma' ;
66import { OrgRole } from '@sourcebot/db' ;
77
88const mocks = vi . hoisted ( ( ) => {
@@ -141,6 +141,48 @@ describe('getAuthenticatedUser', () => {
141141 expect ( user ) . toBeUndefined ( ) ;
142142 } ) ;
143143
144+ test ( 'should return a user object if a valid OAuth Bearer token is present' , async ( ) => {
145+ prisma . oAuthToken . findUnique . mockResolvedValue ( MOCK_OAUTH_TOKEN ) ;
146+ setMockHeaders ( new Headers ( { 'Authorization' : 'Bearer sourcebot-oauth-oauthtoken' } ) ) ;
147+ const user = await getAuthenticatedUser ( ) ;
148+ expect ( user ) . not . toBeUndefined ( ) ;
149+ expect ( user ?. id ) . toBe ( MOCK_USER_WITH_ACCOUNTS . id ) ;
150+ } ) ;
151+
152+ test ( 'should update lastUsedAt when an OAuth Bearer token is used' , async ( ) => {
153+ prisma . oAuthToken . findUnique . mockResolvedValue ( MOCK_OAUTH_TOKEN ) ;
154+ setMockHeaders ( new Headers ( { 'Authorization' : 'Bearer sourcebot-oauth-oauthtoken' } ) ) ;
155+ await getAuthenticatedUser ( ) ;
156+ expect ( prisma . oAuthToken . update ) . toHaveBeenCalledWith ( {
157+ where : { hash : 'oauthtoken' } ,
158+ data : { lastUsedAt : expect . any ( Date ) } ,
159+ } ) ;
160+ } ) ;
161+
162+ test ( 'should return undefined if an OAuth Bearer token is present but the token does not exist' , async ( ) => {
163+ prisma . oAuthToken . findUnique . mockResolvedValue ( null ) ;
164+ setMockHeaders ( new Headers ( { 'Authorization' : 'Bearer sourcebot-oauth-oauthtoken' } ) ) ;
165+ const user = await getAuthenticatedUser ( ) ;
166+ expect ( user ) . toBeUndefined ( ) ;
167+ } ) ;
168+
169+ test ( 'should return undefined if an OAuth Bearer token is present but the token is expired' , async ( ) => {
170+ prisma . oAuthToken . findUnique . mockResolvedValue ( {
171+ ...MOCK_OAUTH_TOKEN ,
172+ expiresAt : new Date ( Date . now ( ) - 1000 ) , // expired 1 second ago
173+ } ) ;
174+ setMockHeaders ( new Headers ( { 'Authorization' : 'Bearer sourcebot-oauth-oauthtoken' } ) ) ;
175+ const user = await getAuthenticatedUser ( ) ;
176+ expect ( user ) . toBeUndefined ( ) ;
177+ } ) ;
178+
179+ test ( 'should not check API key when a sourcebot-oauth- Bearer token is present' , async ( ) => {
180+ prisma . oAuthToken . findUnique . mockResolvedValue ( MOCK_OAUTH_TOKEN ) ;
181+ setMockHeaders ( new Headers ( { 'Authorization' : 'Bearer sourcebot-oauth-oauthtoken' } ) ) ;
182+ await getAuthenticatedUser ( ) ;
183+ expect ( prisma . apiKey . findUnique ) . not . toHaveBeenCalled ( ) ;
184+ } ) ;
185+
144186 test ( 'should return undefined if a Bearer token is present but the user is not found' , async ( ) => {
145187 prisma . user . findUnique . mockResolvedValue ( null ) ;
146188 prisma . apiKey . findUnique . mockResolvedValue ( {
0 commit comments