Skip to content

Commit

Permalink
Updated CI/CD .env, refactored ban tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nidhish-nayak committed Apr 12, 2024
1 parent bf342d0 commit c2b4873
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 41 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ jobs:
echo "S3_BUCKET_LINK=${{ secrets.S3_BUCKET_LINK }}" >> .env
echo "S3_IMAGE_LINK=${{ secrets.S3_IMAGE_LINK }}" >> .env
echo "S3_CLOUDFRONT_LINK=${{ secrets.S3_CLOUDFRONT_LINK }}" >> .env
# Moderator variables
echo "MOD_URL=${{ secrets.MOD_URL }}" >> .env
echo "MOD_KEY=${{ secrets.MOD_KEY }}" >> .env
echo "MOD_HOST=${{ secrets.MOD_HOST }}" >> .env
# Run tests
npm test
- name: Remove .env
Expand Down
106 changes: 65 additions & 41 deletions server/src/__tests__/controllers/moderator/ban.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ describe("Ban moderator test", () => {
app.use("/api/posts", postRoutes);
app.use("/api/stories", storyRoutes);

// Register the user before test
await request(app).post("/api/auth/register").send(userAlt);
});

it("responds 200 for user login", async () => {
const { userId, token } = await manualAuth(
app,
"/api/auth/login",
Expand All @@ -45,7 +49,11 @@ describe("Ban moderator test", () => {
return console.log("Login failed - no token returned!");
user_id = userId;
accessToken = token;
expect(user_id).toBeDefined();
expect(accessToken).toBeDefined();
});

it("adds a new post for mocking ban scenario", async () => {
const { data, error } = await supabase
.from("posts")
.insert({
Expand All @@ -56,55 +64,71 @@ describe("Ban moderator test", () => {
.select();
if (error) return console.log("Post upload to DB has failed!");
post_id = data[0].id;
expect(post_id).toBeDefined();
});

it("bans the user after posting post with unsafe image", async () => {
// Ban user after posting imgUrl
await expect(
moderatorCheck(post_id, "posts", user_id, imgUrl)
).resolves.toBeUndefined();

// Verify user is banned and not able to login
const login = await request(app)
.post("/api/auth/login")
.send(credentials);
expect(login.status).toBe(401);
});

it("removes the user from banned records", async () => {
// Remove banned user
const removeBan = await supabase
.from("bans")
.delete()
.eq("user_id", user_id);
expect(removeBan.status).toBe(204);
});

it("responds 200 for posts ban success", async () => {
// Call moderator - Main test
const res = await moderatorCheck(post_id, "posts", user_id, imgUrl);
expect(res).toBeUndefined();
it("adds a new story for mocking ban scenario", async () => {
const { data, error } = await supabase
.from("stories")
.insert({
img: imgUrl,
userId: user_id,
})
.select();
if (error) return console.log("Story upload to DB has failed!");
story_id = data[0].id;
expect(story_id).toBeDefined();
});

it("bans the user after posting story with unsafe image", async () => {
// Ban user after posting imgUrl
await expect(
moderatorCheck(story_id, "stories", user_id, imgUrl)
).resolves.toBeUndefined();

// Verify user is banned and not able to login
const login = await request(app)
.post("/api/auth/login")
.send(credentials);
expect(login.status).toBe(401);
});

// // Remove banned user
// const { error: banError } = await supabase
// .from("bans")
// .delete()
// .eq("user_id", user_id);
// if (banError) return console.log("User unban failed!");
//
// const { data, error } = await supabase
// .from("stories")
// .insert({
// img: imgUrl,
// userId: user_id,
// })
// .select();
// if (error) return console.log("Story upload to DB has failed!");
//
// // Call moderator - Main test
// await moderatorCheck(data[0].id, "stories", user_id, imgUrl);
//
// const res = await request(app)
// .post("/api/auth/login")
// .send(credentials);
// expect(res.status).toBe(401);
//
// const removeBan = await supabase
// .from("bans")
// .delete()
// .eq("user_id", user_id);
// expect(removeBan.status).toBe(204);
//
// const { status } = await manualAuth(
// app,
// "/api/auth/deregister",
// credentials,
// accessToken
// );
// expect(status).toBe(200);
it("cleanup - remove ban & deregister test user", async () => {
const removeBan = await supabase
.from("bans")
.delete()
.eq("user_id", user_id);
expect(removeBan.status).toBe(204);

const { status } = await manualAuth(
app,
"/api/auth/deregister",
credentials,
accessToken
);
expect(status).toBe(200);
});

afterAll(() => {
Expand Down

0 comments on commit c2b4873

Please sign in to comment.