-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloud-function): getTokensInfoCF (#246)
https://linear.app/valora/issue/ACT-908/createupdate-cloud-function-to-return-new-tokens-info tested by deploying from local machine and invoking from GCP console https://console.cloud.google.com/functions/details/us-central1/getTokensInfoCF?env=gen1&hl=en&project=celo-mobile-alfajores&tab=testing
- Loading branch information
Showing
17 changed files
with
1,142 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file specifies files that are *not* uploaded to Google Cloud | ||
# using gcloud. It follows the same syntax as .gitignore, with the addition of | ||
# "#!include" directives (which insert the entries of the given .gitignore-style | ||
# file at that point). | ||
# | ||
# For more information, run: | ||
# $ gcloud topic gcloudignore | ||
# | ||
.gcloudignore | ||
# If you would like to upload your .git directory, .gitignore file or files | ||
# from your .gitignore file, remove the corresponding line | ||
# below: | ||
.git | ||
.gitignore | ||
|
||
node_modules | ||
#!include:.gitignore | ||
!dist |
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,2 @@ | ||
ENVIRONMENT: 'testnet' | ||
GCLOUD_PROJECT: 'celo-mobile-alfajores' |
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,2 @@ | ||
ENVIRONMENT: 'mainnet' | ||
GCLOUD_PROJECT: 'celo-mobile-mainnet' |
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
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
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,22 @@ | ||
import { _getTokensInfoHttpFunction } from './index' | ||
import mocked = jest.mocked | ||
import { loadCloudFunctionConfig } from './config' | ||
|
||
jest.mock('./config') | ||
|
||
describe('index', () => { | ||
it('_getTokensInfoHttpFunction', async () => { | ||
const req = {} as any | ||
const res = { status: jest.fn().mockReturnThis(), send: jest.fn() } as any | ||
mocked(loadCloudFunctionConfig).mockReturnValue({ | ||
environment: 'mainnet', | ||
gcloudProject: 'celo-mobile-mainnet', | ||
}) | ||
await _getTokensInfoHttpFunction(req, res) | ||
expect(res.status).toHaveBeenCalledWith(200) | ||
expect(res.send).toHaveBeenCalledWith({ | ||
celo: expect.any(Array), | ||
ethereum: expect.any(Array), | ||
}) | ||
}) | ||
}) |
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,21 @@ | ||
import { createLogger, createLoggingMiddleware } from '@valora/logging' | ||
import { ValoraGcloudProject } from './types' | ||
|
||
export const logger = createLogger({ | ||
redact: { | ||
paths: ['req.headers.authorization', 'req.headers.cookie'], | ||
censor: '***REDACTED***', | ||
}, | ||
}) | ||
|
||
let loggingMiddleware: ReturnType<typeof createLoggingMiddleware> | ||
|
||
export const getLoggingMiddleware = (gcloudProject: ValoraGcloudProject) => { | ||
if (!loggingMiddleware) { | ||
loggingMiddleware = createLoggingMiddleware({ | ||
logger, | ||
projectId: gcloudProject, | ||
}) | ||
} | ||
return loggingMiddleware | ||
} |
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
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,40 @@ | ||
import mocked = jest.mocked | ||
import { getLoggingMiddleware, logger } from './log' | ||
import { wrap } from './wrap' | ||
import { Request, Response } from '@google-cloud/functions-framework' | ||
import { asyncHandler } from '@valora/http-handler' | ||
|
||
jest.mock('./log') | ||
jest.mock('@valora/http-handler') | ||
|
||
describe('wrap', () => { | ||
it('wraps a function with logging middleware and async handler', () => { | ||
const mockLoggingMiddleware = jest | ||
.fn() | ||
.mockImplementation((req, res, next) => next(req, res)) // this mock just lets us check that 'next' is set to the httpFunction parameter given to 'wrap' | ||
mocked(getLoggingMiddleware).mockReturnValue(mockLoggingMiddleware) | ||
const mockHttpFunction = jest.fn() | ||
const mockAsyncHttpFunction = jest.fn() | ||
mocked(asyncHandler).mockReturnValue(mockAsyncHttpFunction) | ||
const mockLoadConfig = jest | ||
.fn() | ||
.mockReturnValue({ gcloudProject: 'test-gcloud-project' }) | ||
const wrapped = wrap({ | ||
httpFunction: mockHttpFunction, | ||
loadConfig: mockLoadConfig, | ||
}) | ||
expect(mockLoadConfig).not.toHaveBeenCalled() // don't call loadConfig until the wrapped function is called | ||
expect(asyncHandler).toHaveBeenCalledWith(mockHttpFunction, logger) | ||
const mockReq = {} as Request | ||
const mockRes = {} as Response | ||
wrapped(mockReq, mockRes) | ||
expect(mockLoadConfig).toHaveBeenCalled() | ||
expect(getLoggingMiddleware).toHaveBeenCalledWith('test-gcloud-project') | ||
expect(mockLoggingMiddleware).toHaveBeenCalledWith( | ||
mockReq, | ||
mockRes, | ||
expect.any(Function), | ||
) | ||
expect(mockAsyncHttpFunction).toHaveBeenCalledWith(mockReq, mockRes) | ||
}) | ||
}) |
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,22 @@ | ||
import { | ||
HttpFunction, | ||
Request, | ||
Response, | ||
} from '@google-cloud/functions-framework' | ||
import { getLoggingMiddleware, logger } from './log' | ||
import { ValoraGcloudProject } from './types' | ||
import { asyncHandler } from '@valora/http-handler' | ||
|
||
export function wrap({ | ||
httpFunction, | ||
loadConfig, | ||
}: { | ||
httpFunction: HttpFunction | ||
loadConfig: () => { gcloudProject: ValoraGcloudProject } | ||
}): HttpFunction { | ||
const asyncHttpFunction = asyncHandler(httpFunction, logger) | ||
return (req: Request, res: Response) => { | ||
const loggingMiddleware = getLoggingMiddleware(loadConfig().gcloudProject) | ||
return loggingMiddleware(req, res, () => asyncHttpFunction(req, res)) | ||
} | ||
} |
Oops, something went wrong.