-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
409 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
import { | ||
syncExternalAccounts, | ||
syncIdle7dUsers, | ||
syncIdleUsers, | ||
syncNickNames, | ||
syncOnboarding31dPlusUsers, | ||
syncUnverifiedUsers, | ||
syncUsersStatus, | ||
} from '../../handlers/scheduledEventHandler'; | ||
import { env } from '../../types/global.types'; | ||
import * as apiCallerModule from '../../utils/apiCaller'; | ||
|
||
jest.mock('../../utils/apiCaller', () => ({ | ||
apiCaller: jest.fn(), | ||
})); | ||
|
||
const consoleErrorMock: jest.SpyInstance = jest.spyOn(console, 'error').mockImplementation(); | ||
const apiCallerFunction = apiCallerModule.apiCaller; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
afterAll(() => { | ||
consoleErrorMock.mockRestore(); | ||
}); | ||
|
||
describe('syncUsersStatus', () => { | ||
const mockEnv: env = { | ||
CURRENT_ENVIRONMENT: { | ||
RDS_BASE_API_URL: 'default', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should successfully sync users status', async () => { | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce(undefined); | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce({ | ||
data: { | ||
users: [{ userId: 'asdoiuahow212' }], | ||
}, | ||
}); | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce({ success: true }); | ||
|
||
await syncUsersStatus(mockEnv); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/update', 'PATCH'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status?aggregate=true', 'GET'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/batch', 'PATCH', { | ||
body: JSON.stringify({ users: [{ userId: 'asdoiuahow212' }] }), | ||
}); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(3); | ||
}); | ||
|
||
it('should handle error during users data retrieval', async () => { | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce(undefined); | ||
(apiCallerFunction as jest.Mock).mockRejectedValueOnce(new Error('Error fetching users data')); | ||
|
||
const result = await syncUsersStatus(mockEnv); | ||
|
||
expect(result).toBeNull(); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/update', 'PATCH'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status?aggregate=true', 'GET'); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(2); | ||
|
||
expect(console.error).toHaveBeenCalledWith('Error during syncUsersStatus:', new Error('Error fetching users data')); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
|
||
it('should log an error when no users are found or data is not in the expected format', async () => { | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce(undefined); | ||
(apiCallerFunction as jest.Mock).mockResolvedValueOnce({ | ||
data: { | ||
users: [], | ||
}, | ||
}); | ||
|
||
const result = await syncUsersStatus(mockEnv); | ||
|
||
expect(result).toBeNull(); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status/update', 'PATCH'); | ||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, 'users/status?aggregate=true', 'GET'); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(2); | ||
|
||
expect(console.error).toHaveBeenCalledWith('Error: Users data is not in the expected format or no users found'); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe('sync apis', () => { | ||
const mockEnv: env = { | ||
CURRENT_ENVIRONMENT: { | ||
RDS_BASE_API_URL: 'staging', | ||
}, | ||
}; | ||
|
||
const testSyncFunction = async (syncFunction: Function, endpoint: string, method: string) => { | ||
await syncFunction(mockEnv); | ||
|
||
expect(apiCallerFunction).toHaveBeenCalledWith(mockEnv, endpoint, method); | ||
expect(apiCallerFunction).toHaveBeenCalledTimes(1); | ||
}; | ||
|
||
it('should sync unverified users', async () => { | ||
await testSyncFunction(syncUnverifiedUsers, 'users', 'POST'); | ||
}); | ||
|
||
it('should sync idle users', async () => { | ||
await testSyncFunction(syncIdleUsers, 'discord-actions/group-idle', 'PUT'); | ||
}); | ||
|
||
it('should sync external accounts', async () => { | ||
await testSyncFunction(syncExternalAccounts, 'external-accounts/users?action=discord-users-sync', 'POST'); | ||
}); | ||
|
||
it('should sync nicknames', async () => { | ||
await testSyncFunction(syncNickNames, 'discord-actions/nicknames/sync?dev=true', 'POST'); | ||
}); | ||
|
||
it('should sync idle 7d users', async () => { | ||
await testSyncFunction(syncIdle7dUsers, 'discord-actions/group-idle-7d', 'PUT'); | ||
}); | ||
|
||
it('should sync onboarding 31d+ users', async () => { | ||
await testSyncFunction(syncOnboarding31dPlusUsers, 'discord-actions/group-onboarding-31d-plus', 'PUT'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { RDS_BASE_DEVELOPMENT_API_URL } from '../../constants/urls'; | ||
import { env } from '../../types/global.types'; | ||
import { apiCaller } from '../../utils/apiCaller'; | ||
import { generateJwt } from '../../utils/generateJwt'; | ||
import * as generateJwtModule from '../../utils/generateJwt'; | ||
|
||
jest.mock('../../utils/generateJwt', () => ({ | ||
generateJwt: jest.fn().mockResolvedValue('mocked-token'), | ||
})); | ||
|
||
describe('apiCaller', () => { | ||
const mockEnv: env = { | ||
CURRENT_ENVIRONMENT: { | ||
RDS_BASE_API_URL: 'default', | ||
}, | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
(globalThis as any).fetch = jest.fn(() => | ||
Promise.resolve({ | ||
json: () => Promise.resolve({ success: true }), | ||
}), | ||
); | ||
}); | ||
|
||
it('should make a successful API call', async () => { | ||
const result = await apiCaller(mockEnv, 'users', 'GET'); | ||
expect(generateJwt).toHaveBeenCalledWith(mockEnv); | ||
|
||
expect(result).toEqual({ success: true }); | ||
expect((globalThis as any).fetch).toHaveBeenCalledWith(`${RDS_BASE_DEVELOPMENT_API_URL}/users`, { | ||
method: 'GET', | ||
headers: { | ||
Authorization: 'Bearer mocked-token', | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
}); | ||
|
||
it('should make a successful POST API call', async () => { | ||
const result = await apiCaller(mockEnv, 'test', 'POST', { | ||
body: JSON.stringify({ data: 'example' }), | ||
}); | ||
expect(generateJwt).toHaveBeenCalledWith(mockEnv); | ||
|
||
expect(result).toEqual({ success: true }); | ||
expect((globalThis as any).fetch).toHaveBeenCalledWith(`${RDS_BASE_DEVELOPMENT_API_URL}/test`, { | ||
method: 'POST', | ||
headers: { | ||
Authorization: 'Bearer mocked-token', | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ data: 'example' }), | ||
}); | ||
}); | ||
|
||
it('should log and rethrow error during fetch call failure', async () => { | ||
const mockError = new Error('Network error'); | ||
|
||
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(); | ||
|
||
(globalThis as any).fetch = jest.fn().mockRejectedValue(mockError); | ||
|
||
await expect(apiCaller({}, 'someEndpoint', 'GET')).rejects.toThrowError(mockError); | ||
expect(consoleErrorSpy).toHaveBeenCalledWith(`Error during fetch operation: ${mockError}`); | ||
expect(consoleErrorSpy).toHaveBeenCalledTimes(1); | ||
|
||
consoleErrorSpy.mockRestore(); | ||
}); | ||
|
||
it('should handle the case where generateJwt returns undefined and throw an error', async () => { | ||
const generateJwtMock = jest.spyOn(generateJwtModule, 'generateJwt'); | ||
generateJwtMock.mockImplementationOnce(() => Promise.reject(new Error('Generate JWT error'))); | ||
|
||
await expect(apiCaller(mockEnv, 'someEndpoint', 'GET')).rejects.toThrow('Generate JWT error'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.