-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* ci: modify github workflow to run sq analysis * test: add example lambda tests * ci: run sq analysis only when push to main * fix: typo
- Loading branch information
1 parent
82940da
commit baef6a3
Showing
8 changed files
with
270 additions
and
19 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,44 @@ | ||
name: Serverless Boilerplate | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
tests: | ||
runs-on: ubuntu-latest | ||
name: Build | ||
strategy: | ||
matrix: | ||
node-version: [20] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- run: cp .env.dist .env | ||
- run: npm ci | ||
- run: npm run lint | ||
- run: npm run test-coverage | ||
- name: Upload artifacts | ||
uses: actions/upload-artifact@v2 | ||
with: | ||
name: coverage | ||
path: coverage/ | ||
|
||
sonarqube-analysis: | ||
runs-on: ubuntu-latest | ||
needs: tests | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
- name: Download artifact | ||
uses: actions/download-artifact@v2 | ||
with: | ||
name: coverage | ||
path: coverage/ | ||
- name: SonarQube Scan | ||
uses: sonarsource/sonarqube-scan-action@master | ||
env: | ||
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} | ||
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} |
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 |
---|---|---|
@@ -1,19 +1,90 @@ | ||
import request from "supertest"; | ||
import sinon from "sinon"; | ||
import { DataSource, Repository } from "typeorm"; | ||
import { lambdaHandler } from "../handler"; | ||
import assert from "assert"; | ||
import { StatusCodes } from "http-status-codes"; | ||
|
||
describe("test endpoint", () => { | ||
const server = request("http://localhost:1337/"); | ||
let sandbox: sinon.SinonSandbox; | ||
let userRepositoryStub: sinon.SinonStub; | ||
let dataSourceStub: sinon.SinonStub; | ||
let metadataStub: sinon.SinonStub; | ||
|
||
describe("Test obligatory query parameter", () => { | ||
it("GET `dev/?exampleParam=test` returns 200", () => { | ||
return server.get("dev/users").query("exampleParam=test").expect(200); | ||
}); | ||
const data = [ | ||
{ | ||
id: "50ece88a-61ac-4435-8457-051693716276", | ||
firstName: "John", | ||
lastName: "Doe", | ||
email: "[email protected]", | ||
}, | ||
{ | ||
id: "8eb91b5f-63ef-4ca8-9db4-3fa6f173f1ae", | ||
firstName: "Mark", | ||
lastName: "Smith", | ||
email: "[email protected]", | ||
} | ||
]; | ||
|
||
const pagination = { | ||
page: 1, | ||
limit: 3, | ||
total: 2, | ||
totalPages: 1 | ||
}; | ||
|
||
const params = { | ||
exampleParam: "example", | ||
page: "1", | ||
limit: "3", | ||
}; | ||
|
||
it("GET `dev` returns bad request", () => { | ||
return server.get("dev/users").expect(400); | ||
beforeEach(() => { | ||
sandbox = sinon.createSandbox(); | ||
userRepositoryStub = sandbox.stub(Repository.prototype, "findAndCount").resolves([data, 2]); | ||
dataSourceStub = sandbox.stub(DataSource.prototype, "initialize").resolves(); | ||
metadataStub = sandbox.stub(Repository.prototype, "metadata"); | ||
}); | ||
|
||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
describe("GET dev/users", () => { | ||
it("should return users", async () => { | ||
metadataStub.value({ | ||
columns: [{ propertyName: "email", }, { propertyName: "firstName" }] | ||
}); | ||
const filters = { | ||
sort: { email: "ASC" }, | ||
filter: { firstName: "John" }, | ||
search: "Doe", | ||
}; | ||
|
||
const response = await lambdaHandler({ | ||
queryStringParameters: { ...params, ...filters } | ||
}); | ||
|
||
const parsedResponse = JSON.parse(response.body); | ||
assert.equal(response.statusCode, StatusCodes.OK); | ||
assert.deepEqual(parsedResponse, { meta: { pagination, ...filters }, data }); | ||
}); | ||
|
||
it("GET 'any' returns not found", () => { | ||
return server.get("any").expect(404); | ||
it("shouldn't return filters if they are not available", async () => { | ||
metadataStub.value({ | ||
columns: [{ propertyName: "id", }, { propertyName: "lastName" }] | ||
}); | ||
|
||
const response = await lambdaHandler({ | ||
queryStringParameters: { | ||
...params, | ||
sort: { email: "ASC", id: "ASC" }, | ||
filter: { firstName: "John", lastName: "Doe" }, | ||
} | ||
}); | ||
|
||
const parsedResponse = JSON.parse(response.body); | ||
assert.equal(response.statusCode, StatusCodes.OK); | ||
assert.deepEqual(parsedResponse, { meta: { pagination, sort: { id: "ASC" }, filter: { lastName: "Doe" }}, data }); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.