-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/org-persistence
- Loading branch information
Showing
4 changed files
with
163 additions
and
192 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
10 changes: 3 additions & 7 deletions
10
src/core/infrastructure/midaz/transactions/midaz-fetch-all-transactions-repository.ts
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
298 changes: 146 additions & 152 deletions
298
src/core/infrastructure/utils/http-fetch-utils.test.ts
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 |
---|---|---|
@@ -1,152 +1,146 @@ | ||
import { getServerSession } from 'next-auth' | ||
import { nextAuthCasdoorOptions } from '../next-auth/casdoor/next-auth-casdoor-provider' | ||
import { handleMidazError } from './midaz-error-handler' | ||
import { | ||
httpMidazAuthFetch, | ||
HTTP_METHODS, | ||
HttpFetchOptions | ||
} from './http-fetch-utils' | ||
|
||
jest.mock('next-auth', () => ({ | ||
getServerSession: jest.fn() | ||
})) | ||
|
||
jest.mock('../next-auth/casdoor/next-auth-casdoor-provider', () => ({ | ||
nextAuthCasdoorOptions: {} | ||
})) | ||
|
||
jest.mock('./midaz-error-handler', () => ({ | ||
handleMidazError: jest.fn(() => { | ||
throw new Error('Error occurred') | ||
}) | ||
})) | ||
|
||
global.fetch = jest.fn() | ||
|
||
describe('httpMidazAuthFetch', () => { | ||
const mockSession = { | ||
user: { | ||
access_token: 'mock_access_token' | ||
} | ||
} | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
;(getServerSession as jest.Mock).mockResolvedValue(mockSession) | ||
}) | ||
|
||
it('should fetch data successfully', async () => { | ||
const mockResponse = { data: 'test' } | ||
;(fetch as jest.Mock).mockResolvedValue({ | ||
body: '', | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
ok: true | ||
}) | ||
|
||
const httpFetchOptions: HttpFetchOptions = { | ||
url: 'https://api.example.com/data', | ||
method: HTTP_METHODS.GET | ||
} | ||
|
||
const result = await httpMidazAuthFetch(httpFetchOptions) | ||
|
||
expect(getServerSession).toHaveBeenCalledWith(nextAuthCasdoorOptions) | ||
expect(fetch).toHaveBeenCalledWith(httpFetchOptions.url, { | ||
method: httpFetchOptions.method, | ||
body: httpFetchOptions.body, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${mockSession.user.access_token}` | ||
} | ||
}) | ||
expect(result).toEqual(mockResponse) | ||
}) | ||
|
||
it('should make a POST request with the correct headers and body', async () => { | ||
const mockResponse = { ok: true, json: jest.fn().mockResolvedValue({}) } | ||
;(fetch as jest.Mock).mockResolvedValue({ | ||
body: '', | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
ok: true | ||
}) | ||
|
||
const options: HttpFetchOptions = { | ||
url: 'https://api.example.com/data', | ||
method: HTTP_METHODS.POST, | ||
body: JSON.stringify({ key: 'value' }) | ||
} | ||
|
||
const response = await httpMidazAuthFetch(options) | ||
|
||
expect(getServerSession).toHaveBeenCalledWith(nextAuthCasdoorOptions) | ||
expect(fetch).toHaveBeenCalledWith(options.url, { | ||
method: options.method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${mockSession.user.access_token}` | ||
}, | ||
body: options.body | ||
}) | ||
expect(response).toBe(mockResponse) | ||
}) | ||
|
||
it('should include additional headers if provided', async () => { | ||
const mockResponse = { ok: true, json: jest.fn().mockResolvedValue({}) } | ||
;(fetch as jest.Mock).mockResolvedValue({ | ||
body: '', | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
ok: true | ||
}) | ||
|
||
const options: HttpFetchOptions = { | ||
url: 'https://api.example.com/data', | ||
method: HTTP_METHODS.GET, | ||
headers: { | ||
'Custom-Header': 'CustomValue' | ||
} | ||
} | ||
|
||
const response = await httpMidazAuthFetch(options) | ||
|
||
expect(getServerSession).toHaveBeenCalledWith(nextAuthCasdoorOptions) | ||
expect(fetch).toHaveBeenCalledWith(options.url, { | ||
method: options.method, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${mockSession.user.access_token}`, | ||
'Custom-Header': 'CustomValue' | ||
}, | ||
body: undefined | ||
}) | ||
expect(response).toBe(mockResponse) | ||
}) | ||
|
||
it('should handle errors when fetching data', async () => { | ||
const mockErrorResponse = { message: 'Error occurred' } | ||
;(fetch as jest.Mock).mockResolvedValue({ | ||
body: '', | ||
json: jest.fn().mockResolvedValue(mockErrorResponse), | ||
ok: false | ||
}) | ||
|
||
const httpFetchOptions: HttpFetchOptions = { | ||
url: 'https://api.example.com/data', | ||
method: HTTP_METHODS.GET | ||
} | ||
|
||
await expect(httpMidazAuthFetch(httpFetchOptions)).rejects.toThrow( | ||
'Error occurred' | ||
) | ||
|
||
expect(getServerSession).toHaveBeenCalledWith(nextAuthCasdoorOptions) | ||
expect(fetch).toHaveBeenCalledWith(httpFetchOptions.url, { | ||
method: httpFetchOptions.method, | ||
body: httpFetchOptions.body, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: `Bearer ${mockSession.user.access_token}` | ||
} | ||
}) | ||
expect(handleMidazError).toHaveBeenCalledWith(mockErrorResponse) | ||
}) | ||
}) | ||
import { LoggerAggregator } from '@/core/application/logger/logger-aggregator' | ||
import { getServerSession } from 'next-auth' | ||
import { MidazRequestContext } from '../logger/decorators/midaz-id' | ||
import { HTTP_METHODS, MidazHttpFetchUtils } from './http-fetch-utils' | ||
import { handleMidazError } from './midaz-error-handler' | ||
|
||
jest.mock('next-auth', () => ({ | ||
getServerSession: jest.fn() | ||
})) | ||
|
||
jest.mock('./midaz-error-handler', () => ({ | ||
handleMidazError: jest.fn(() => { | ||
throw new Error('Error occurred') | ||
}) | ||
})) | ||
|
||
jest.mock('../next-auth/casdoor/next-auth-casdoor-provider', () => ({ | ||
nextAuthCasdoorOptions: {} | ||
})) | ||
|
||
describe('MidazHttpFetchUtils', () => { | ||
let midazHttpFetchUtils: MidazHttpFetchUtils | ||
let midazRequestContext: MidazRequestContext | ||
let midazLogger: LoggerAggregator | ||
|
||
beforeEach(() => { | ||
midazRequestContext = { | ||
getMidazId: jest.fn().mockReturnValue('test-request-id') | ||
} as unknown as MidazRequestContext | ||
|
||
midazLogger = { | ||
error: jest.fn(), | ||
info: jest.fn() | ||
} as unknown as LoggerAggregator | ||
|
||
midazHttpFetchUtils = new MidazHttpFetchUtils( | ||
midazRequestContext, | ||
midazLogger | ||
) | ||
}) | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
|
||
it('should successfully fetch data', async () => { | ||
const mockResponse = { data: 'test' } | ||
const mockFetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
body: true, | ||
status: 200 | ||
}) | ||
global.fetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
body: true, | ||
status: 200 | ||
}) | ||
;(getServerSession as jest.Mock).mockResolvedValue({ | ||
user: { access_token: 'test-token' } | ||
}) | ||
|
||
const result = await midazHttpFetchUtils.httpMidazAuthFetch({ | ||
url: 'https://api.example.com/test', | ||
method: HTTP_METHODS.GET | ||
}) | ||
|
||
expect(result).toEqual(mockResponse) | ||
expect(midazLogger.info).toHaveBeenCalledWith( | ||
'[INFO] - httpMidazAuthFetch ', | ||
{ | ||
url: 'https://api.example.com/test', | ||
method: 'GET', | ||
status: 200 | ||
} | ||
) | ||
}) | ||
|
||
it('should handle fetch error', async () => { | ||
const mockErrorResponse = { error: 'test error' } | ||
const mockFetch = jest.fn().mockResolvedValue({ | ||
ok: false, | ||
json: jest.fn().mockResolvedValue(mockErrorResponse), | ||
body: true, | ||
status: 400 | ||
}) | ||
global.fetch = mockFetch | ||
;(getServerSession as jest.Mock).mockResolvedValue({ | ||
user: { access_token: 'test-token' } | ||
}) | ||
;(handleMidazError as jest.Mock).mockImplementation(() => { | ||
throw new Error('Handled error') | ||
}) | ||
|
||
await expect( | ||
midazHttpFetchUtils.httpMidazAuthFetch({ | ||
url: 'https://api.example.com/test', | ||
method: HTTP_METHODS.GET | ||
}) | ||
).rejects.toThrow('Handled error') | ||
|
||
expect(midazLogger.error).toHaveBeenCalledWith( | ||
'[ERROR] - httpMidazAuthFetch ', | ||
{ | ||
url: 'https://api.example.com/test', | ||
method: 'GET', | ||
status: 400, | ||
response: mockErrorResponse | ||
} | ||
) | ||
}) | ||
|
||
it('should set the correct headers', async () => { | ||
const mockResponse = { data: 'test' } | ||
const mockFetch = jest.fn().mockResolvedValue({ | ||
ok: true, | ||
json: jest.fn().mockResolvedValue(mockResponse), | ||
body: true, | ||
status: 200 | ||
}) | ||
global.fetch = mockFetch | ||
;(getServerSession as jest.Mock).mockResolvedValue({ | ||
user: { access_token: 'test-token' } | ||
}) | ||
|
||
await midazHttpFetchUtils.httpMidazAuthFetch({ | ||
url: 'https://api.example.com/test', | ||
method: HTTP_METHODS.GET, | ||
headers: { | ||
'Custom-Header': 'CustomValue' | ||
} | ||
}) | ||
|
||
expect(mockFetch).toHaveBeenCalledWith('https://api.example.com/test', { | ||
method: 'GET', | ||
body: undefined, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
Authorization: 'Bearer test-token', | ||
'X-Request-Id': 'test-request-id', | ||
'Custom-Header': 'CustomValue' | ||
} | ||
}) | ||
}) | ||
}) |
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