Skip to content

Commit 67342e1

Browse files
Add a patch URL to change nickname (#44)
* add a patch url to change nickname * add JWT Verification * Update from develop and better coding practices * add tests for nickname change * improve test coverage * remove console.log * Change api name to a noun * change api name to /guild/members * Change response message of name changed
1 parent b684af1 commit 67342e1

File tree

5 files changed

+101
-6
lines changed

5 files changed

+101
-6
lines changed

src/constants/responses.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export const RETRY_COMMAND =
2323
"Oops, we didn't catch that! Please use the command again.";
2424

2525
export const ROLE_ADDED = "Role added successfully";
26+
27+
export const NAME_CHANGED = "User nickname changed successfully";

src/controllers/changeNickname.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { IRequest } from "itty-router";
2+
import { env } from "../typeDefinitions/default.types";
3+
import JSONResponse from "../utils/JsonResponse";
4+
import * as response from "../constants/responses";
5+
import { verifyAuthToken } from "../utils/verifyAuthToken";
6+
import { updateNickName } from "../utils/updateNickname";
7+
8+
export async function changeNickname(request: IRequest, env: env) {
9+
const authHeader = await request.headers.get("Authorization");
10+
11+
if (!authHeader) {
12+
return new JSONResponse(response.BAD_SIGNATURE);
13+
}
14+
15+
try {
16+
await verifyAuthToken(authHeader, env);
17+
const { discordId, userName } = await request.json();
18+
const res = await updateNickName(discordId, userName, env);
19+
return new JSONResponse(res);
20+
} catch {
21+
return new JSONResponse(response.BAD_SIGNATURE);
22+
}
23+
}

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
createGuildRoleHandler,
1212
} from "./controllers/guildRoleHandler";
1313
import { getMembersInServerHandler } from "./controllers/getMembersInServer";
14+
import { changeNickname } from "./controllers/changeNickname";
1415

1516
const router = Router();
1617

@@ -20,6 +21,8 @@ router.get("/", async () => {
2021
});
2122
});
2223

24+
router.patch("/guild/member", changeNickname);
25+
2326
router.put("/roles/create", createGuildRoleHandler);
2427

2528
router.put("/roles/add", addGroupRoleHandler);

src/utils/updateNickname.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { INTERNAL_SERVER_ERROR } from "../constants/responses";
1+
import { INTERNAL_SERVER_ERROR, NAME_CHANGED } from "../constants/responses";
22
import { DISCORD_BASE_URL } from "../constants/urls";
33
import { env } from "../typeDefinitions/default.types";
44

@@ -19,13 +19,9 @@ export async function updateNickName(
1919
body: JSON.stringify(data),
2020
});
2121
if (nameChangeResponse.ok) {
22-
return await nameChangeResponse.json();
23-
} else {
24-
console.log(nameChangeResponse.json());
25-
return INTERNAL_SERVER_ERROR;
22+
return NAME_CHANGED;
2623
}
2724
} catch (error) {
28-
console.log(error);
2925
return INTERNAL_SERVER_ERROR;
3026
}
3127
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import {
2+
INTERNAL_SERVER_ERROR,
3+
NAME_CHANGED,
4+
} from "../../../src/constants/responses";
5+
import { DISCORD_BASE_URL } from "../../../src/constants/urls";
6+
import JSONResponse from "../../../src/utils/JsonResponse";
7+
import { updateNickName } from "../../../src/utils/updateNickname";
8+
9+
describe("Update nickname", () => {
10+
const mockEnv = {
11+
BOT_PUBLIC_KEY: "xyz",
12+
DISCORD_GUILD_ID: "123",
13+
DISCORD_TOKEN: "abc",
14+
};
15+
16+
const mockData = { discordId: "12345678910111213", nickname: "jhon" };
17+
18+
test("updatenickname fetch is called with expected parameters", async () => {
19+
const data = { nick: mockData.nickname };
20+
21+
jest
22+
.spyOn(global, "fetch")
23+
.mockImplementation(() =>
24+
Promise.resolve(new JSONResponse(NAME_CHANGED))
25+
);
26+
27+
const response = await updateNickName(
28+
mockData.discordId,
29+
mockData.nickname,
30+
mockEnv
31+
);
32+
33+
expect(global.fetch).toHaveBeenCalledWith(
34+
`${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/${mockData.discordId}`,
35+
{
36+
method: "PATCH",
37+
headers: {
38+
"Content-Type": "application/json",
39+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
40+
},
41+
body: JSON.stringify(data),
42+
}
43+
);
44+
45+
expect(response).toEqual(NAME_CHANGED);
46+
});
47+
48+
test("Return Internal server error for improper body", async () => {
49+
jest
50+
.spyOn(global, "fetch")
51+
.mockRejectedValue(() =>
52+
Promise.resolve(new JSONResponse({ INTERNAL_SERVER_ERROR }))
53+
);
54+
55+
const response = await updateNickName(mockData.discordId, "", mockEnv);
56+
57+
expect(response).toEqual(INTERNAL_SERVER_ERROR);
58+
59+
expect(global.fetch).toHaveBeenCalledWith(
60+
`${DISCORD_BASE_URL}/guilds/${mockEnv.DISCORD_GUILD_ID}/members/${mockData.discordId}`,
61+
{
62+
method: "PATCH",
63+
headers: {
64+
"Content-Type": "application/json",
65+
Authorization: `Bot ${mockEnv.DISCORD_TOKEN}`,
66+
},
67+
body: JSON.stringify({ nick: "" }),
68+
}
69+
);
70+
});
71+
});

0 commit comments

Comments
 (0)