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

PER-9718 [back-end] Endpoint for admins to create environment configu… #137

Merged
merged 2 commits into from
Oct 25, 2024
Merged
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
193 changes: 183 additions & 10 deletions packages/api/src/feature_flag/controller.test.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import request from "supertest";
import type { NextFunction, Request } from "express";
import { logger } from "@stela/logger";
import createError from "http-errors";
import { db } from "../database";
import { app } from "../app";
import { extractUserIsAdminFromAuthToken } from "../middleware";
import type { FeatureFlagRequest } from "./models";
import {
extractUserIsAdminFromAuthToken,
verifyAdminAuthentication,
} from "../middleware";
import type { CreateFeatureFlagRequest, FeatureFlagRequest } from "./models";

jest.mock("../database");
jest.mock("../middleware");
jest.mock("@stela/logger");

describe("GET /feature-flags", () => {
const agent = request(app);
const loadFixtures = async (): Promise<void> => {
await db.sql("fixtures.create_test_feature_flags");
};

const loadFixtures = async (): Promise<void> => {
await db.sql("fixtures.create_test_feature_flags");
};
const clearDatabase = async (): Promise<void> => {
await db.query("TRUNCATE feature_flag CASCADE");
};

const clearDatabase = async (): Promise<void> => {
await db.query("TRUNCATE feature_flag CASCADE");
};
describe("GET /feature-flags", () => {
const agent = request(app);

beforeEach(async () => {
await clearDatabase();
Expand Down Expand Up @@ -110,3 +114,172 @@ describe("GET /feature-flags", () => {
expect(response.body).toEqual(expected);
});
});

describe("POST /feature-flag", () => {
const agent = request(app);
beforeEach(async () => {
(verifyAdminAuthentication as jest.Mock).mockImplementation(
(req: Request, __, next: NextFunction) => {
(req.body as CreateFeatureFlagRequest).emailFromAuthToken =
"test@permanent.org";
(req.body as CreateFeatureFlagRequest).adminSubjectFromAuthToken =
"6b640c73-4963-47de-a096-4a05ff8dc5f5";
next();
}
);
jest.restoreAllMocks();
jest.clearAllMocks();
await loadFixtures();
await clearDatabase();
});

afterEach(async () => {
jest.restoreAllMocks();
jest.clearAllMocks();
await clearDatabase();
});

test("should respond with a 200 status code", async () => {
await agent
.post("/api/v2/feature-flags")
.send({
name: "TEST",
description: "description",
})
.expect(200);
});

test("should respond with a 400 if feature flag already exists", async () => {
await agent
.post("/api/v2/feature-flags")
.send({
name: "TEST",
description: "description",
})
.expect(200);

// trying to create again the same feature flag should fail
await agent
.post("/api/v2/feature-flags")
.send({
name: "TEST",
description: "description",
})
.expect(400);
});

test("should respond with 401 status code if lacking admin authentication", async () => {
(verifyAdminAuthentication as jest.Mock).mockImplementation(
(_: Request, __, next: NextFunction) => {
next(new createError.Unauthorized("You aren't logged in"));
}
);
await agent.post("/api/v2/feature-flags").expect(401);
});

test("should respond with 400 status code if missing emailFromAuthToken", async () => {
(verifyAdminAuthentication as jest.Mock).mockImplementation(
(req: Request, __, next: NextFunction) => {
(req.body as CreateFeatureFlagRequest).adminSubjectFromAuthToken =
"6b640c73-4963-47de-a096-4a05ff8dc5f5";
next();
}
);
await agent
.post("/api/v2/feature-flags")
.send({
name: "TEST",
description: "description",
})
.expect(400);
});

test("should respond with 400 status code if emailFromAuthToken is not a string", async () => {
(verifyAdminAuthentication as jest.Mock).mockImplementation(
(req: Request, __, next: NextFunction) => {
(req.body as { emailFromAuthToken: number }).emailFromAuthToken = 123;
(req.body as CreateFeatureFlagRequest).adminSubjectFromAuthToken =
"6b640c73-4963-47de-a096-4a05ff8dc5f5";
next();
}
);
await agent
.post("/api/v2/feature-flags")
.send({
name: "TEST",
description: "description",
})
.expect(400);
});

test("should respond with 400 status name if code is missing", async () => {
await agent
.post("/api/v2/feature-flags")
.send({
description: "description",
})
.expect(400);
});

test("should respond with 400 status code if name is not a string", async () => {
await agent
.post("/api/v2/feature-flags")
.send({
name: 123,
description: "description",
})
.expect(400);
});

test("should store the new feature flag in the database", async () => {
await agent
.post("/api/v2/feature-flags")
.send({
name: "name",
description: "description",
})
.expect(200);
const result = await db.query(
`SELECT
name,
description,
globally_enabled::boolean as "globallyEnabled"
FROM
feature_flag
WHERE
name = 'name'`
);
expect(result.rows.length).toBe(1);
expect(result.rows[0]).toEqual({
name: "name",
description: "description",
globallyEnabled: false,
});
});

test("should respond with 500 if the database call fails", async () => {
jest.spyOn(db, "sql").mockImplementation(() => {
throw new Error("SQL error");
});
await agent
.post("/api/v2/feature-flags")
.send({
name: "name",
description: "description",
})
.expect(500);
});

test("should log the error if the database call fails", async () => {
const testError = new Error("SQL error");
jest.spyOn(db, "sql").mockRejectedValueOnce(testError);
await agent
.post("/api/v2/feature-flags")
.send({
name: "name",
description: "description",
})
.expect(500);
expect(logger.error).toHaveBeenCalled();
});
});
27 changes: 26 additions & 1 deletion packages/api/src/feature_flag/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import type { Request, Response, NextFunction } from "express";

import { logger } from "@stela/logger";
import { featureService } from "./service";
import { extractUserIsAdminFromAuthToken } from "../middleware";
import { createFeatureService } from "./service/create";
import {
extractUserIsAdminFromAuthToken,
verifyAdminAuthentication,
} from "../middleware";
import { validateCreateFeatureFlagRequest } from "./validators";
import { validateIsAdminFromAuthentication } from "../validators/shared";
import { isValidationError } from "../validators/validator_util";

Expand All @@ -27,3 +32,23 @@ featureController.get(
}
}
);

featureController.post(
"/",
verifyAdminAuthentication,
async (req: Request, res: Response, next: NextFunction) => {
try {
validateCreateFeatureFlagRequest(req.body);
const insertedFeatureFlag = await createFeatureService.createFeatureFlag(
req.body
);
res.status(200).send({ data: insertedFeatureFlag });
} catch (err) {
if (isValidationError(err)) {
res.status(400).json({ error: err.message });
return;
}
next(err);
}
}
);
7 changes: 7 additions & 0 deletions packages/api/src/feature_flag/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ export interface FeatureFlagNameRow {
name: string;
}

export interface CreateFeatureFlagRequest {
emailFromAuthToken: string;
adminSubjectFromAuthToken: string;
name: string;
description: string;
}

export interface FeatureFlagRequest {
admin: boolean;
}
15 changes: 15 additions & 0 deletions packages/api/src/feature_flag/queries/create_feature_flag.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
INSERT INTO feature_flag (
name,
description,
globally_enabled
) VALUES (
:name,
:description,
:globally_enabled
) RETURNING
id,
name,
description,
globally_enabled AS "globallyEnabled",
created_at AS "createdAt",
updated_at AS "updatedAt";
41 changes: 41 additions & 0 deletions packages/api/src/feature_flag/service/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { logger } from "@stela/logger";
import createError from "http-errors";
import { TinyPgError } from "tinypg";
import type { CreateFeatureFlagRequest, FeatureFlagRow } from "../models";
import { db } from "../../database";

const duplicateFeatureFlagError =
'duplicate key value violates unique constraint "feature_flag_name_unique"';

export const createFeatureFlag = async (
featureFlagData: CreateFeatureFlagRequest
): Promise<FeatureFlagRow> => {
const result = await db
.sql<FeatureFlagRow>("feature_flag.queries.create_feature_flag", {
name: featureFlagData.name,
description: featureFlagData.description,
globally_enabled: false,
})
.catch((err) => {
logger.error(err);
if (
err instanceof TinyPgError &&
err.message === duplicateFeatureFlagError
) {
throw new createError.BadRequest("Feature flag already exists");
}
throw new createError.InternalServerError(
"Failed to create feature flag"
);
});

if (result.rows[0] === undefined) {
throw new createError.InternalServerError("Failed to create feature flag");
}

return result.rows[0];
};

export const createFeatureService = {
createFeatureFlag,
};
Loading
Loading