Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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 services/discordService.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const logger = require("../utils/logger");
const firestore = require("../utils/firestore");
const { fetchAllUsers } = require("../models/users");
const { generateAuthTokenForCloudflare, generateCloudFlareHeaders } = require("../utils/discord-actions");
const logger = require("../utils/logger");
const config = require("config");
const userModel = firestore.collection("users");
const DISCORD_BASE_URL = config.get("services.discordBot.baseUrl");

Expand Down
1 change: 1 addition & 0 deletions services/logService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import firestore from "../utils/firestore";
const logsModel = firestore.collection("logs");
import admin from "firebase-admin";
const { INTERNAL_SERVER_ERROR } = require("../constants/errorMessages");
const logger = require("../utils/logger");

interface LogMeta {
userId?: string;
Expand Down
152 changes: 152 additions & 0 deletions test/integration/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chai from "chai";
import chaiHttp from "chai-http";
const { expect } = chai;
import config from "config";
import sinon from "sinon";
const app = require("../../server");
const addUser = require("../utils/addUser");
const cleanDb = require("../utils/cleanDb");
Expand Down Expand Up @@ -65,6 +66,7 @@ describe("Application", function () {

after(async function () {
await cleanDb();
sinon.restore();
});

describe("GET /applications", function () {
Expand Down Expand Up @@ -488,4 +490,154 @@ describe("Application", function () {
});
});
});

describe("PATCH /applications/:applicationId/nudge", function () {
let nudgeApplicationId: string;

beforeEach(async function () {
const applicationData = { ...applicationsData[0], userId };
nudgeApplicationId = await applicationModel.addApplication(applicationData);
});

afterEach(async function () {
sinon.restore();
});

it("should successfully nudge a pending application when user owns it and no previous nudge exists", function (done) {
chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(200);
expect(res.body.message).to.be.equal("Application nudged successfully");
expect(res.body.nudgeCount).to.be.equal(1);
expect(res.body.lastNudgeAt).to.be.a("string");
done();
});
});

it("should successfully nudge an application when 24 hours have passed since last nudge", function (done) {
chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(200);
expect(res.body.nudgeCount).to.be.equal(1);

const twentyFiveHoursAgo = new Date(Date.now() - 25 * 60 * 60 * 1000).toISOString();
applicationModel.updateApplication({ lastNudgeAt: twentyFiveHoursAgo }, nudgeApplicationId).then(() => {
chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(200);
expect(res.body.message).to.be.equal("Application nudged successfully");
expect(res.body.nudgeCount).to.be.equal(2);
expect(res.body.lastNudgeAt).to.be.a("string");
done();
});
});
});
});

it("should return 404 if the application doesn't exist", function (done) {
chai
.request(app)
.patch(`/applications/non-existent-id/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(404);
expect(res.body.error).to.be.equal("Not Found");
expect(res.body.message).to.be.equal("Application not found");
done();
});
});

it("should return 401 if user is not authenticated", function (done) {
chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(401);
expect(res.body.error).to.be.equal("Unauthorized");
expect(res.body.message).to.be.equal("Unauthenticated User");
done();
});
});

it("should return 401 if user does not own the application", function (done) {
chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.set("cookie", `${cookieName}=${secondUserJwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(401);
expect(res.body.error).to.be.equal("Unauthorized");
expect(res.body.message).to.be.equal("You are not authorized to nudge this application");
done();
});
});

it("should return 429 when trying to nudge within 24 hours", function (done) {
chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(200);

chai
.request(app)
.patch(`/applications/${nudgeApplicationId}/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(429);
expect(res.body.error).to.be.equal("Too Many Requests");
expect(res.body.message).to.be.equal(
"Cannot nudge application. Please wait 24 hours since the last nudge."
);
done();
});
});
});

it("should return 400 when trying to nudge an application that is not in pending status", function (done) {
const nonPendingApplicationData = { ...applicationsData[1], userId };
applicationModel.addApplication(nonPendingApplicationData).then((nonPendingApplicationId: string) => {
chai
.request(app)
.patch(`/applications/${nonPendingApplicationId}/nudge`)
.set("cookie", `${cookieName}=${jwt}`)
.end(function (err, res) {
if (err) return done(err);

expect(res).to.have.status(400);
expect(res.body.error).to.be.equal("Bad Request");
expect(res.body.message).to.be.equal(
"Application is not pending. You can only nudge pending applications."
);
done();
});
});
});
});
});
Loading
Loading