This file contains hidden or 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
/** | |
* First we need to install mocha, chai and their types with command: | |
* npm install --save-dev @types/mocha @types/chai mocha chai | |
*/ | |
import "mocha"; | |
describe("test iwfp api", () => { | |
it("hello world no crash", () => { | |
// run some tests here |
This file contains hidden or 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
import funcTest from "firebase-functions-test"; | |
describe("test iwfp api", () => { | |
let iwfpapi, adminInitStub; | |
const tester = funcTest(); | |
before(async () => { | |
adminInitStub = sinon.stub(admin, "initializeApp"); | |
iwfpapi = await import("../src/index"); | |
}); |
This file contains hidden or 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
describe("test iwfp api", () => { | |
// prepare code shown in the last section. | |
it("hello world no crash", () => { | |
const wrapped = tester.wrap(iwfpapi.helloWorld); | |
wrapped(null); | |
}); | |
}); |
This file contains hidden or 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
{ | |
"name": "example test", | |
"scripts": { | |
"test": "mocha -r ts-node/register --reporter spec test/**/*.ts" | |
} | |
} | |
// To run the tests, just use: npm test |
This file contains hidden or 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
export async function lintWorkspace() { | |
try { | |
const workspaceDir = util.getGitHubWorkspace(); | |
const workspaceFiles: string[] = util.getLintFileList(workspaceDir); | |
const reportsMetadata = new Array(); | |
for (const workspaceFile of workspaceFiles) { | |
if (util.isReadmeFilename(workspaceFile)) { | |
const readmeFileContent = util.readFileContent(workspaceFile); | |
const relativePath = path.relative(workspaceDir, workspaceFile); | |
const reportEntry = { |
This file contains hidden or 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
export const internalAwesomeAPI = functions.https.onCall(async (data, fbContext) => { | |
const context: FunctionContext = provider.fbContext2context(fbContext); | |
const intermediateRequest: IntermediateRequestInterface = parseIntermediateRequest(data); | |
await awesomeHandler(intermediateRequest, context); | |
}); |
This file contains hidden or 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
export const externalAwesomeAPI = functions.https.onRequest(async (req, res) => { | |
if (req.method !== "POST") { | |
return res.sendStatus(403); | |
} | |
const context: FunctionContext = await token2context(req.body.token); | |
const intermediateRequest: IntermediateRequestInterface = parseIntermediateRequest(req.body); | |
try { | |
await awesomeHandler(intermediateRequest, context); | |
return res.sendStatus(200); | |
} catch (err) { |
This file contains hidden or 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
async function awesomeHandler(req: any, context: FunctionContext) { | |
if (context.authenticated) { | |
const userUid: string = context.uid; | |
// Here goes your API implementation | |
} else { | |
throw UnauthenticatedUserError; | |
} | |
} |
This file contains hidden or 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
export interface FunctionContext { | |
authenticated: boolean; | |
uid: string; | |
} |
This file contains hidden or 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
async token2context(token: string): Promise<FunctionContext> { | |
const context: FunctionContext = { authenticated: false, uid: "na" }; | |
try { | |
const verifyResult = await this.auth.verifyIdToken(token); | |
context.authenticated = true; | |
context.uid = verifyResult.uid; | |
} catch (err) { | |
console.log(err); | |
} | |
return context; |