Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create new project: test #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .depcheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ignores: ["eslint", "@types/eslint", "@vitest/coverage-v8", "ts-node"]
skip-missing: true
13 changes: 13 additions & 0 deletions .env.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#LOCAL DEV
#If you want to start this api locally create a .env from this

NEW_RELIC_APP_NAME=local
NEW_RELIC_NO_CONFIG_FILE=true
NEW_RELIC_ENABLED=false
LOG_LEVEL=info
FINGERS_CROSSED_LOG_LEVEL=info
APP_ENV=dev
APP_NAME=test
APP_TEAM=team-apex
GITHUB_REF=refs/heads/master
GITHUB_SHA=cb1a4f5afbd920acb80e3774437990f63ec25856
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* @Flaconi/team-apex @Flaconi/ci
46 changes: 46 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
version: 2
registries:
npm-npmjs:
type: npm-registry
url: https://registry.npmjs.org
replaces-base: true
token:
github-flaconi-ci:
type: git
url: https://github.com
username: x-access-token
password:
updates:
- package-ecosystem: npm
directory: "/"
schedule:
interval: daily
time: "08:00"
timezone: Europe/Berlin
open-pull-requests-limit: 10
registries:
- npm-npmjs
ignore:
- dependency-name: "node-fetch"
versions: ["3.x"]
groups:
vi-test:
patterns:
- "@vitest*"
- "vitest"
aws-sdk:
patterns:
- "@aws-sdk*"
- "aws-sdk*"
types:
patterns:
- "@types*"
- package-ecosystem: github-actions
directory: "/"
schedule:
interval: weekly
day: monday
time: "08:00"
timezone: Europe/Berlin
registries:
- github-flaconi-ci
34 changes: 34 additions & 0 deletions .github/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Configuration for Release Drafter: https://github.com/toolmantim/release-drafter
template: |
## Changes

$CHANGES
name-template: '$RESOLVED_VERSION 🌈'
tag-template: '$RESOLVED_VERSION'
categories:
- title: '🚀 Features'
labels:
- feature
- enhancement
- title: '🐛 Bug Fixes'
labels:
- fix
- bugfix
- bug
- title: '🧰 Maintenance'
labels:
- chore
- dependencies
change-template: '- $TITLE @$AUTHOR (#$NUMBER)'
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks.
version-resolver:
major:
labels:
- 'major'
minor:
labels:
- 'minor'
patch:
labels:
- 'patch'
default: patch
9 changes: 9 additions & 0 deletions .github/workflows/auto-merge.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
name: Dependabot auto-merge

on: pull_request

jobs:
auto-merge:
uses: Flaconi/github-actions/.github/workflows/auto-merge.yml@v1
secrets: inherit
17 changes: 17 additions & 0 deletions .github/workflows/backups.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
name: Backup Repository

on:
push:
branches:
- master

jobs:
backup:
uses: Flaconi/github-reusable-workflow/.github/workflows/backups.yml@v1
with:
enabled: True
region: eu-central-1
secrets:
iam_role_arn:
bucket_name:
29 changes: 29 additions & 0 deletions .github/workflows/manual.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Manual deployment
on:
workflow_dispatch:
inputs:
env:
description: "Environment to deploy"
required: true
default: "dev"
type: choice
options:
- dev
- stage
- prod
jobs:
build:
uses: Flaconi/github-actions/.github/workflows/build-node.yml@v1
with:
node_version: "22"
secrets: inherit
deploy:
name: "Deploy: "
needs: build
uses: Flaconi/github-actions/.github/workflows/deploy-node.yml@v1
secrets: inherit
with:
env:
smoke: false
node_version: "22"
12 changes: 12 additions & 0 deletions .github/workflows/release-drafter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: Release Drafter

on:
push:
branches:
- master

jobs:
update_release_draft:
uses: Flaconi/github-actions/.github/workflows/release-drafter.yml@v1
secrets: inherit
19 changes: 19 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
name: Release deploy
on:
release:
types: [published]
jobs:
build:
uses: Flaconi/github-actions/.github/workflows/build-node.yml@v1
with:
node_version: "22"
secrets: inherit
prod:
needs: build
uses: Flaconi/github-actions/.github/workflows/deploy-node.yml@v1
secrets: inherit
with:
env: prod
node_version: "22"
smoke: false
29 changes: 29 additions & 0 deletions .github/workflows/test-and-deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
name: Test and Deploy
on:
push:
branches: ["**"]
tags-ignore: ["**"]
jobs:
build:
uses: Flaconi/github-actions/.github/workflows/build-node.yml@v1
with:
node_version: "22"
secrets: inherit
dev:
needs: build
uses: Flaconi/github-actions/.github/workflows/deploy-node.yml@v1
secrets: inherit
with:
env: dev
node_version: "22"
smoke: false
stage:
if: github.ref_name == 'master'
needs: build
uses: Flaconi/github-actions/.github/workflows/deploy-node.yml@v1
secrets: inherit
with:
env: stage
node_version: "22"
smoke: false
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.idea/
.build
.eslintcache
.serverless
node_modules
yarn-error.log
coverage
.env
newrelic_agent.log
.npmrc

.yarn/*
!.yarn/cache
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
.pnp*
934 changes: 934 additions & 0 deletions .yarn/releases/yarn-4.6.0.cjs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
nodeLinker: node-modules

yarnPath: .yarn/releases/yarn-4.6.0.cjs
17 changes: 17 additions & 0 deletions __mocks__/newrelic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Event } from 'newrelic';

let events: Event[] = [];

const recordCustomEvent = (name: string, attributes: { [keys: string]: boolean | number | string }): void => {
events.push({ name, attributes });
};

const __getRecordedEvents = (): Event[] => {
return events;
};

const __reset = (): void => {
events = [];
};

export default { recordCustomEvent, __getRecordedEvents, __reset };
11 changes: 11 additions & 0 deletions __tests__/unit/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
process.env = Object.assign(process.env, {
LOG_LEVEL: process.env.LOG_LEVEL || 'info',
FINGERS_CROSSED_LOG_LEVEL: 'warn',
NEW_RELIC_EVENT_TYPE: 'testEvent',
METRICS_TAG_ENVIRONMENT: 'test',
GITHUB_REF: 'refs/tags/v1.0.0',
GITHUB_SHA: '1111111',
APP_ENV: 'test',
APP_TEAM: 'team-apex',
APP_NAME: 'test',
});
95 changes: 95 additions & 0 deletions __tests__/unit/handler.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import FlaconiLogger from '@flaconi/node-logger';
import requestTest from 'supertest';
import { describe, test, it, expect, vi } from 'vitest';

import * as newrelicMocks from '../../__mocks__/newrelic';
import { configureApp } from '../../src/app';
import * as Handler from '../../src/handler';

describe('handle notifications', () => {
const table = [
{
scope: undefined, description: 'emptyScope', secured: false,
},
];
test.each(table)('should respond with 200 for $description', async ({ scope, secured }) => {
const expectResponse = {
boilerplate: 'test', scope, secured,
};
Object.keys(expectResponse).forEach(key => expectResponse[key] === undefined ? delete expectResponse[key] : {});

await requestTest(configureApp())
.get('/path?locale=de-DE')
.set('x-correlation-id', 'frontastiRequestId')
.expect('Content-Type', /json/)
.expect(200)
.expect(expectResponse);

expect(newrelicMocks.default.__getRecordedEvents().length).toBe(1);
expect(newrelicMocks.default.__getRecordedEvents()).toEqual(expect.arrayContaining([
expect.objectContaining({
name: 'testEvent',
attributes: {
metricName: 'example.success',
value: 1,
environment: 'test',
},
}),
]));
});

it('should respond with 500', async () => {
let customRequestHandlerWasCalled = false;

await requestTest(configureApp([
(
_req, _res, next,
) => {
customRequestHandlerWasCalled = true;
next();
},
]))
.get('/path?errorType=fetch')
.set('x-correlation-id', 'frontastiRequestId')
.set('accept-encoding', 'other') // to make sure it run not in the compression filter method
.expect(500)
.expect({});

expect(customRequestHandlerWasCalled).toBeTruthy();
expect(newrelicMocks.default.__getRecordedEvents().length).toBe(1);
expect(newrelicMocks.default.__getRecordedEvents()).toEqual(expect.arrayContaining([
expect.objectContaining({
name: 'testEvent',
attributes: {
metricName: 'example.failure',
type: 'fetch',
value: 1,
environment: 'test',
},
}),
]));
});

it('should respond with 404', async () => {
let customRequestHandlerWasCalled = false;
vi.spyOn(Handler, 'default').mockImplementation(async () => {
throw new Error('error');
});

const rootLogger = vi.spyOn(FlaconiLogger, 'resetFingersCrossedTransport');

await requestTest(configureApp([
(
_req, _res, next,
) => {
customRequestHandlerWasCalled = true;
next();
},
]))
.get('/')
.expect(500);

expect(customRequestHandlerWasCalled).toBeTruthy();
expect(rootLogger).toHaveBeenCalled();
});
});
15 changes: 15 additions & 0 deletions __tests__/unit/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { afterEach, beforeEach, vi } from 'vitest';

import * as newrelicMocks from '../../__mocks__/newrelic';
import './env';
vi.mock('newrelic', () => {
return newrelicMocks;
});

beforeEach(() => {
newrelicMocks.default.__reset();
});

afterEach(() => {
vi.clearAllMocks();
});
Loading