Skip to content

Commit d44e773

Browse files
committed
Added more tests
1 parent 7e7014e commit d44e773

File tree

5 files changed

+205
-1
lines changed

5 files changed

+205
-1
lines changed

nyc.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,8 @@ module.exports = {
66
'check-coverage': true,
77
extension: ['.js', '.ts'],
88
reporter: ['html', 'text-summary'],
9-
include: ['src/**/*']
9+
include: ['src/**/*'],
10+
exclude: [
11+
'src/admin/functions/**/*'
12+
]
1013
}

src/shared/functions/FunctionsError.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class FunctionsError extends Error implements Flattable<FunctionsError.Fl
5050
}
5151
}
5252

53+
// istanbul ignore next
5354
export namespace FunctionsError {
5455

5556
export type Flatten = {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { expect } from '@assertive-ts/core';
2+
import { FunctionsError, FunctionsErrorCode } from '../../src/shared/functions/FunctionsError';
3+
4+
describe('FunctionsError', () => {
5+
6+
it('should create an instance of FunctionsError', () => {
7+
const error = new FunctionsError('invalid-argument', 'Invalid argument provided');
8+
expect(error).toBeInstanceOf(FunctionsError);
9+
expect(error.name).toBeEqual('FunctionsError');
10+
expect(error.code).toBeEqual('invalid-argument');
11+
expect(error.message).toBeEqual('Invalid argument provided');
12+
expect(error.details).toBeNull();
13+
});
14+
15+
it('should flatten the error correctly', () => {
16+
const error = new FunctionsError('not-found', 'Resource not found', 'Details about the error');
17+
const flattened = error.flatten;
18+
expect(flattened).toBeEqual({
19+
name: 'FunctionsError',
20+
code: 'not-found',
21+
message: 'Resource not found',
22+
details: 'Details about the error'
23+
});
24+
});
25+
26+
it('should build an error from flattened object', () => {
27+
const flattened: FunctionsError.Flatten = {
28+
name: 'FunctionsError',
29+
code: 'permission-denied',
30+
message: 'Permission denied',
31+
details: 'Details about the error'
32+
};
33+
const builder = new FunctionsError.TypeBuilder();
34+
const error = builder.build(flattened);
35+
expect(error).toBeInstanceOf(FunctionsError);
36+
expect(error.name).toBeEqual('FunctionsError');
37+
expect(error.code).toBeEqual('permission-denied');
38+
expect(error.message).toBeEqual('Permission denied');
39+
expect(error.details).toBeEqual('Details about the error');
40+
});
41+
42+
it('should validate FunctionsErrorCode correctly', () => {
43+
expect(FunctionsErrorCode.isFunctionsErrorCode('ok')).toBeTrue();
44+
expect(FunctionsErrorCode.isFunctionsErrorCode('invalid-argument')).toBeTrue();
45+
expect(FunctionsErrorCode.isFunctionsErrorCode('unknown-code')).toBeFalse();
46+
});
47+
});
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import { expect } from '@assertive-ts/core';
2+
import { FunctionsLogger } from '../../src/admin/logger/FunctionsLogger';
3+
import * as logger from 'firebase-functions/logger';
4+
import { capture, spy } from 'ts-mockito';
5+
6+
describe('FunctionsLogger', () => {
7+
let functionsLogger: FunctionsLogger;
8+
let spiedLoggger: typeof logger;
9+
10+
beforeEach(() => {
11+
functionsLogger = new FunctionsLogger();
12+
spiedLoggger = spy(logger);
13+
});
14+
15+
it('should log a debug message', () => {
16+
functionsLogger.log('debug', true, 'testFunction', 'test description', { key: 'value' });
17+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
18+
severity: 'DEBUG',
19+
message: '[DEBUG: testFunction] test description | {\n\tkey: \'value\'\n}'
20+
});
21+
});
22+
23+
it('should log an info message', () => {
24+
functionsLogger.log('info', true, 'testFunction', 'test description', { key: 'value' });
25+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
26+
severity: 'INFO',
27+
message: '[INFO: testFunction] test description | {\n\tkey: \'value\'\n}'
28+
});
29+
});
30+
31+
it('should log a warnin message', () => {
32+
functionsLogger.log('notice', true, 'testFunction', 'test description', { key: 'value' });
33+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
34+
severity: 'NOTICE',
35+
message: '[NOTICE: testFunction] test description | {\n\tkey: \'value\'\n}'
36+
});
37+
});
38+
39+
it('should log a warning message', () => {
40+
functionsLogger.log('warning', true, 'testFunction', 'test description', { key: 'value' });
41+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
42+
severity: 'WARNING',
43+
message: '[WARNING: testFunction] test description | {\n\tkey: \'value\'\n}'
44+
});
45+
});
46+
47+
it('should log an error message', () => {
48+
functionsLogger.log('error', true, 'testFunction', 'test description', { key: 'value' });
49+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
50+
severity: 'ERROR',
51+
message: '[ERROR: testFunction] test description | {\n\tkey: \'value\'\n}'
52+
});
53+
});
54+
55+
it('should log a critical message', () => {
56+
functionsLogger.log('critical', true, 'testFunction', 'test description', { key: 'value' });
57+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
58+
severity: 'CRITICAL',
59+
message: '[CRITICAL: testFunction] test description | {\n\tkey: \'value\'\n}'
60+
});
61+
});
62+
63+
it('should log an alert message', () => {
64+
functionsLogger.log('alert', true, 'testFunction', 'test description', { key: 'value' });
65+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
66+
severity: 'ALERT',
67+
message: '[ALERT: testFunction] test description | {\n\tkey: \'value\'\n}'
68+
});
69+
});
70+
71+
it('should log an emergency message', () => {
72+
functionsLogger.log('emergency', true, 'testFunction', 'test description', { key: 'value' });
73+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
74+
severity: 'EMERGENCY',
75+
message: '[EMERGENCY: testFunction] test description | {\n\tkey: \'value\'\n}'
76+
});
77+
});
78+
79+
it('should log a message with null description and details', () => {
80+
functionsLogger.log('info', true, 'testFunction', null, null);
81+
expect(capture(spiedLoggger.write).first()[0]).toBeEqual({
82+
severity: 'INFO',
83+
message: '[INFO: testFunction]'
84+
});
85+
});
86+
});

test/functions/macTag.test.ts

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { expect } from '@assertive-ts/core';
2+
import { createMacTag } from '../../src/client/functions/createMacTag';
3+
import { verifyMacTag } from '../../src/admin/functions/verifyMacTag';
4+
5+
describe('MAC Tag Functions', () => {
6+
const key = new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]);
7+
8+
describe('createMacTag', () => {
9+
it('should create a valid MAC tag for given parameters', () => {
10+
const parameters = { foo: 'bar' };
11+
const tag = createMacTag(parameters, key);
12+
expect(tag).toBeOfType('string');
13+
expect(tag.length).toBeGreaterThan(0);
14+
});
15+
16+
it('should create different MAC tags for different parameters', () => {
17+
const parameters1 = { foo: 'bar' };
18+
const parameters2 = { foo: 'baz' };
19+
const tag1 = createMacTag(parameters1, key);
20+
const tag2 = createMacTag(parameters2, key);
21+
expect(tag1).not.toBeEqual(tag2);
22+
});
23+
24+
it('should create the same MAC tag for the same parameters and key', () => {
25+
const parameters = { foo: 'bar' };
26+
const tag1 = createMacTag(parameters, key);
27+
const tag2 = createMacTag(parameters, key);
28+
expect(tag1).toBeEqual(tag2);
29+
});
30+
31+
it('should handle undefined parameters', () => {
32+
const tag = createMacTag(undefined, key);
33+
expect(tag).toBeOfType('string');
34+
expect(tag.length).toBeGreaterThan(0);
35+
});
36+
});
37+
38+
describe('verifyMacTag', () => {
39+
it('should verify a valid MAC tag', () => {
40+
const parameters = { foo: 'bar' };
41+
const tag = createMacTag(parameters, key);
42+
const isValid = verifyMacTag(tag, parameters, key);
43+
expect(isValid).toBeTrue();
44+
});
45+
46+
it('should not verify an invalid MAC tag', () => {
47+
const parameters = { foo: 'bar' };
48+
const invalidTag = 'invalidtag';
49+
const isValid = verifyMacTag(invalidTag, parameters, key);
50+
expect(isValid).toBeFalse();
51+
});
52+
53+
it('should not verify a MAC tag with different parameters', () => {
54+
const parameters1 = { foo: 'bar' };
55+
const parameters2 = { foo: 'baz' };
56+
const tag = createMacTag(parameters1, key);
57+
const isValid = verifyMacTag(tag, parameters2, key);
58+
expect(isValid).toBeFalse();
59+
});
60+
61+
it('should handle undefined parameters', () => {
62+
const tag = createMacTag(undefined, key);
63+
const isValid = verifyMacTag(tag, undefined, key);
64+
expect(isValid).toBeTrue();
65+
});
66+
});
67+
});

0 commit comments

Comments
 (0)