refactor: update admin apikey for dashboard support

This commit is contained in:
Jeeiii
2024-03-27 16:31:05 +01:00
parent e496fd6fd5
commit fb852d3f10
7 changed files with 125 additions and 129 deletions

View File

@@ -1,9 +1,9 @@
import { Body, Controller, Get, Param, Post, Put } from "@nestjs/common"
import { ApiCreatedResponse } from "@nestjs/swagger"
import { ApiKeyActions } from "@bandada/utils"
import { CreateAdminDTO } from "./dto/create-admin.dto"
import { AdminsService } from "./admins.service"
import { Admin } from "./entities/admin.entity"
import { UpdateApiKeyDTO } from "./dto/update-apikey.dto"
@Controller("admins")
export class AdminsController {
@@ -23,8 +23,8 @@ export class AdminsController {
@Put(":admin/apikey")
async updateApiKey(
@Param("admin") adminId: string,
@Body() action: ApiKeyActions
@Body() dto: UpdateApiKeyDTO
): Promise<string> {
return this.adminsService.updateApiKey(adminId, action)
return this.adminsService.updateApiKey(adminId, dto.action)
}
}

View File

@@ -81,10 +81,10 @@ describe("AdminsService", () => {
describe("# updateApiKey", () => {
it("Should create an apikey for the admin", async () => {
const apiKey = await adminsService.updateApiKey({
adminId: hashedId,
action: ApiKeyActions.Generate
})
const apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
admin = await adminsService.findOne({ id: hashedId })
@@ -95,10 +95,10 @@ describe("AdminsService", () => {
it("Should generate another apikey for the admin", async () => {
const previousApiKey = admin.apiKey
const apiKey = await adminsService.updateApiKey({
adminId: hashedId,
action: ApiKeyActions.Generate
})
const apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
admin = await adminsService.findOne({ id: hashedId })
@@ -110,10 +110,7 @@ describe("AdminsService", () => {
it("Should disable the apikey for the admin", async () => {
const { apiKey } = admin
await adminsService.updateApiKey({
adminId: hashedId,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(hashedId, ApiKeyActions.Disable)
admin = await adminsService.findOne({ id: hashedId })
@@ -124,10 +121,7 @@ describe("AdminsService", () => {
it("Should enable the apikey for the admin", async () => {
const { apiKey } = admin
await adminsService.updateApiKey({
adminId: hashedId,
action: ApiKeyActions.Enable
})
await adminsService.updateApiKey(hashedId, ApiKeyActions.Enable)
admin = await adminsService.findOne({ id: hashedId })
@@ -138,10 +132,10 @@ describe("AdminsService", () => {
it("Should not create the apikey when the given id does not belog to an admin", async () => {
const wrongId = "wrongId"
const fun = adminsService.updateApiKey({
adminId: wrongId,
action: ApiKeyActions.Disable
})
const fun = adminsService.updateApiKey(
wrongId,
ApiKeyActions.Disable
)
await expect(fun).rejects.toThrow(
`The '${wrongId}' does not belong to an admin`
@@ -154,10 +148,10 @@ describe("AdminsService", () => {
address: "address2"
})
const fun = adminsService.updateApiKey({
adminId: tempAdmin.id,
action: ApiKeyActions.Enable
})
const fun = adminsService.updateApiKey(
tempAdmin.id,
ApiKeyActions.Enable
)
await expect(fun).rejects.toThrow(
`The '${tempAdmin.id}' does not have an apikey`
@@ -167,11 +161,11 @@ describe("AdminsService", () => {
it("Shoul throw if the action does not exist", async () => {
const wrongAction = "wrong-action"
const fun = adminsService.updateApiKey({
adminId: hashedId,
const fun = adminsService.updateApiKey(
hashedId,
// @ts-ignore
action: wrongAction
})
wrongAction
)
await expect(fun).rejects.toThrow(
`Unsupported ${wrongAction} apikey`

View File

@@ -0,0 +1,7 @@
import { ApiKeyActions } from "@bandada/utils"
import { IsEnum } from "class-validator"
export class UpdateApiKeyDTO {
@IsEnum(ApiKeyActions)
action: ApiKeyActions
}

View File

@@ -11,22 +11,27 @@ import { OAuthAccount } from "./entities/credentials-account.entity"
import { CredentialsService } from "./credentials.service"
import { AdminsModule } from "../admins/admins.module"
jest.mock("@bandada/utils", () => ({
__esModule: true,
getBandadaContract: () => ({
updateGroups: () => ({
status: true,
logs: ["1"]
jest.mock("@bandada/utils", () => {
const originalModule = jest.requireActual("@bandada/utils")
return {
__esModule: true,
...originalModule,
getBandadaContract: () => ({
updateGroups: () => ({
status: true,
logs: ["1"]
}),
getGroups: () => []
}),
getGroups: () => []
}),
blockchainCredentialSupportedNetworks: [
{
id: "sepolia",
name: "Sepolia"
}
]
}))
blockchainCredentialSupportedNetworks: [
{
id: "sepolia",
name: "Sepolia"
}
]
}
})
jest.mock("@bandada/credentials", () => ({
__esModule: true,

View File

@@ -14,17 +14,22 @@ import { Admin } from "../admins/entities/admin.entity"
import { CreateGroupDto } from "./dto/create-group.dto"
import { UpdateGroupDto } from "./dto/update-group.dto"
jest.mock("@bandada/utils", () => ({
__esModule: true,
getBandadaContract: () => ({
updateGroups: jest.fn(() => ({
status: true,
logs: ["1"]
})),
getGroups: jest.fn(() => []),
updateFingerprintDuration: jest.fn(() => null)
})
}))
jest.mock("@bandada/utils", () => {
const originalModule = jest.requireActual("@bandada/utils")
return {
__esModule: true,
...originalModule,
getBandadaContract: () => ({
updateGroups: jest.fn(() => ({
status: true,
logs: ["1"]
})),
getGroups: jest.fn(() => []),
updateFingerprintDuration: jest.fn(() => null)
})
}
})
describe("GroupsService", () => {
let groupsService: GroupsService
@@ -400,10 +405,10 @@ describe("GroupsService", () => {
address: "0x"
})
apiKey = await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Generate
})
apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
admin = await adminsService.findOne({ id: admin.id })
})
@@ -501,10 +506,7 @@ describe("GroupsService", () => {
})
it("Should not create a group if the API key is disabled for the admin", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Disable)
const fun = groupsService.createGroupWithAPIKey(
groupDto,
@@ -518,10 +520,7 @@ describe("GroupsService", () => {
})
it("Should not remove a group if the API key is disabled for the admin", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Enable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Enable)
const group = await groupsService.createGroupWithAPIKey(
groupDto,
@@ -529,10 +528,7 @@ describe("GroupsService", () => {
apiKey
)
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Disable)
const fun = groupsService.removeGroupWithAPIKey(
group.id,
@@ -546,10 +542,7 @@ describe("GroupsService", () => {
})
it("Should not remove a group if the given id does not belong to the group admin", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Enable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Enable)
const group = await groupsService.createGroupWithAPIKey(
groupDto,
@@ -562,10 +555,10 @@ describe("GroupsService", () => {
address: "0x02"
})
const anotherApiKey = await adminsService.updateApiKey({
adminId: anotherAdmin.id,
action: ApiKeyActions.Generate
})
const anotherApiKey = await adminsService.updateApiKey(
anotherAdmin.id,
ApiKeyActions.Generate
)
anotherAdmin = await adminsService.findOne({ id: anotherAdmin.id })
@@ -615,10 +608,10 @@ describe("GroupsService", () => {
address: "0x"
})
apiKey = await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Generate
})
apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
admin = await adminsService.findOne({ id: admin.id })
})
@@ -726,10 +719,7 @@ describe("GroupsService", () => {
})
it("Should not create the groups if the API key is disabled for the admin", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Disable)
const fun = groupsService.createGroupsWithAPIKey(
groupsDtos,
@@ -760,10 +750,10 @@ describe("GroupsService", () => {
address: "0x02"
})
const anotherApiKey = await adminsService.updateApiKey({
adminId: anotherAdmin.id,
action: ApiKeyActions.Generate
})
const anotherApiKey = await adminsService.updateApiKey(
anotherAdmin.id,
ApiKeyActions.Generate
)
anotherAdmin = await adminsService.findOne({ id: anotherAdmin.id })
@@ -803,11 +793,10 @@ describe("GroupsService", () => {
address: "0x"
})
apiKey = await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Generate
})
apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
admin = await adminsService.findOne({ id: admin.id })
group = await groupsService.createGroup(groupDto, admin.id)
})
@@ -872,10 +861,7 @@ describe("GroupsService", () => {
})
it("Should not update a group if the API key is disabled", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Disable)
const fun = groupsService.updateGroupWithApiKey(
groupId,
@@ -901,10 +887,10 @@ describe("GroupsService", () => {
address: "0x"
})
apiKey = await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Generate
})
apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
group = await groupsService.createGroup(
{
@@ -1014,10 +1000,7 @@ describe("GroupsService", () => {
})
it("Should not add a member to an existing group if API is disabled", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Disable)
const fun = groupsService.addMemberWithAPIKey(
group.id,
@@ -1054,10 +1037,10 @@ describe("GroupsService", () => {
address: "0x"
})
apiKey = await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Generate
})
apiKey = await adminsService.updateApiKey(
admin.id,
ApiKeyActions.Generate
)
group = await groupsService.createGroup(
{
@@ -1173,10 +1156,7 @@ describe("GroupsService", () => {
})
it("Should not add a member to an existing group if API is disabled", async () => {
await adminsService.updateApiKey({
adminId: admin.id,
action: ApiKeyActions.Disable
})
await adminsService.updateApiKey(admin.id, ApiKeyActions.Disable)
const fun = groupsService.addMembersWithAPIKey(
group.id,

View File

@@ -9,16 +9,21 @@ import { Invite } from "./entities/invite.entity"
import { InvitesService } from "./invites.service"
import { AdminsModule } from "../admins/admins.module"
jest.mock("@bandada/utils", () => ({
__esModule: true,
getBandadaContract: () => ({
updateGroups: jest.fn(() => ({
status: true,
logs: ["1"]
})),
getGroups: jest.fn(() => [])
})
}))
jest.mock("@bandada/utils", () => {
const originalModule = jest.requireActual("@bandada/utils")
return {
__esModule: true,
...originalModule,
getBandadaContract: () => ({
updateGroups: jest.fn(() => ({
status: true,
logs: ["1"]
})),
getGroups: jest.fn(() => [])
})
}
})
describe("InvitesService", () => {
let invitesService: InvitesService