Skip to content

Commit

Permalink
[@dhealthdapps/backend] fix(test): add validate challenge scheduler t…
Browse files Browse the repository at this point in the history
…ests
  • Loading branch information
kravchenkodhealth authored and evias committed Jan 3, 2023
1 parent 15f042e commit bc8ea8e
Show file tree
Hide file tree
Showing 3 changed files with 188 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export class ValidateChallengeScheduler {
} catch (err) {
// if challenge isn't on chain - print info to the console
this.logger.error("failed to validate challenge", err);
throw err;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EventEmitter2 } from "@nestjs/event-emitter";
import { SchedulerRegistry } from "@nestjs/schedule";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { getModelToken } from "@nestjs/mongoose";

// internal dependencies
import { ValidateChallengeScheduler } from "../../../../src/common/schedulers/ValidateChallengeScheduler";
Expand All @@ -24,7 +25,6 @@ import { AccountsService } from "../../../../src/common/services/AccountsService
import { ChallengesService } from "../../../../src/common/services/ChallengesService";
import { QueryService } from "../../../../src/common/services/QueryService";
import { MockModel } from "../../../mocks/global";
import { getModelToken } from "@nestjs/mongoose";

describe("common/AuthGateway", () => {
let authGateway: AuthGateway;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
/**
* This file is part of dHealth dApps Framework shared under LGPL-3.0
* Copyright (C) 2022-present dHealth Network, All rights reserved.
*
* @package dHealth dApps Framework
* @subpackage Backend
* @author dHealth Network <[email protected]>
* @license LGPL-3.0
*/

// external dependencies
import { TestingModule, Test } from "@nestjs/testing";
import { SchedulerRegistry } from "@nestjs/schedule";
import { ConfigService } from "@nestjs/config";
import { JwtService } from "@nestjs/jwt";
import { EventEmitter2 } from "@nestjs/event-emitter";
import { getModelToken } from "@nestjs/mongoose";

// internal dependencies
import { AuthService } from "../../../../src/common/services/AuthService";
import { NetworkService } from "../../../../src/common/services/NetworkService";
import { AccountsService } from "../../../../src/common/services/AccountsService";
import { ChallengesService } from "../../../../src/common/services/ChallengesService";
import { QueryService } from "../../../../src/common/services/QueryService";
import { MockModel } from "../../../mocks/global";
import { ValidateChallengeScheduler } from "../../../../src/common/schedulers/ValidateChallengeScheduler";

describe("common/ValidateChallengeScheduler", () => {
let validateChallengeScheduler: ValidateChallengeScheduler;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
ValidateChallengeScheduler,
SchedulerRegistry,
EventEmitter2,
AuthService,
ConfigService,
NetworkService,
AccountsService,
ChallengesService,
QueryService,
JwtService,
{
provide: getModelToken("Account"),
useValue: MockModel,
}, // requirement from AccountsService
{
provide: getModelToken("AuthChallenge"),
useValue: MockModel,
},
{
provide: getModelToken("AccountSession"),
useValue: MockModel,
}, // requirement from AuthService
],
}).compile();

validateChallengeScheduler = module.get<ValidateChallengeScheduler>(
ValidateChallengeScheduler,
);
});

it("should be defined", () => {
expect(validateChallengeScheduler).toBeDefined();
});

describe("constructor()", () => {
it("should initialize cronjob", () => {
expect((validateChallengeScheduler as any).job).not.toBe(null);
expect((validateChallengeScheduler as any).job).not.toBe(undefined);
});

it("should add cron to nest registry", () => {
const mockedFn = jest.fn();

(validateChallengeScheduler as any).schedulerRegistry = {
addCronJob: mockedFn,
};

new ValidateChallengeScheduler(
(validateChallengeScheduler as any).schedulerRegistry,
{} as AuthService,
{} as EventEmitter2,
);

expect(mockedFn).toBeCalledTimes(1);
});

it("should initialize logger", () => {
expect((validateChallengeScheduler as any).logger).not.toBe(null);
expect((validateChallengeScheduler as any).logger).not.toBe(undefined);
});
});

describe("startCronJob()", () => {
it("should start cronjob", () => {
const mockFn = jest.fn();

(validateChallengeScheduler as any).job = {
start: mockFn,
};

validateChallengeScheduler.startCronJob("someFakeChallenge");

expect(mockFn).toBeCalledTimes(1);
});

it("should assign argument to challenge prop", () => {
validateChallengeScheduler.startCronJob("someFakeChallenge");

expect((validateChallengeScheduler as any).challenge).toBe(
"someFakeChallenge",
);
});

it("should start stop timeout", () => {
validateChallengeScheduler.startCronJob("someFakeChallenge");

expect((validateChallengeScheduler as any).stopCronJobTimeout).not.toBe(
null,
);
expect((validateChallengeScheduler as any).stopCronJobTimeout).not.toBe(
undefined,
);
});
});

describe("stopCronJob()", () => {
it("should stop cronjob", () => {
const mockFn = jest.fn();
(validateChallengeScheduler as any).job = {
stop: mockFn,
};

(validateChallengeScheduler as any).stopCronJob();

expect(mockFn).toBeCalledTimes(1);
});

it("should reset challenge string", () => {
(validateChallengeScheduler as any).stopCronJob();

expect((validateChallengeScheduler as any).challenge).toBe("");
});

it("should clear stopTimeout", () => {
const clearTimeout = jest.fn();
(validateChallengeScheduler as any).startCronJob();
(validateChallengeScheduler as any).stopCronJob();

expect((validateChallengeScheduler as any).stopCronJobTimeout).not.toBe(
undefined,
);
});
});

describe("validate()", () => {
it("should call validateChallenge", () => {
const mockedValidate = jest.fn();

(validateChallengeScheduler as any).authService = {
validateChallenge: mockedValidate,
};

(validateChallengeScheduler as any).validate();

expect(mockedValidate).toBeCalled();
});

it("should log error if challenge invalid", () => {
(validateChallengeScheduler as any).challenge = "invalidChallenge";

expect((validateChallengeScheduler as any).validate()).toThrowError();
});

// it("should stop cronJob", () => {
// const mockedFn = jest.fn();
// (validateChallengeScheduler as any).stopCronJob = mockedFn;

// (validateChallengeScheduler as any).validate();

// expect(mockedFn).toBeCalled();
// });
});
});

0 comments on commit bc8ea8e

Please sign in to comment.