Skip to content

Commit

Permalink
Merge pull request #536 from waddaboo/issue#535
Browse files Browse the repository at this point in the history
Add more tests to improve test coverage
  • Loading branch information
vplasencia authored Aug 6, 2024
2 parents f590a81 + 0ee28f3 commit 9aa38db
Show file tree
Hide file tree
Showing 4 changed files with 464 additions and 2 deletions.
199 changes: 198 additions & 1 deletion apps/api/src/app/credentials/credentials.service.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getProvider, validateCredentials } from "@bandada/credentials"
import {
getProvider,
validateCredentials,
validateManyCredentials
} from "@bandada/credentials"
import { ScheduleModule } from "@nestjs/schedule"
import { Test } from "@nestjs/testing"
import { TypeOrmModule } from "@nestjs/typeorm"
Expand Down Expand Up @@ -177,6 +181,37 @@ describe("CredentialsService", () => {
await expect(fun).rejects.toThrow(`OAuth state does not exist`)
})

it("Should throw an error if the credential group blockchain network is not supported", async () => {
const { id: _groupId } = await groupsService.createGroup(
{
name: "Group2",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600,
credentials: JSON.stringify({
id: "BLOCKCHAIN_TRANSACTIONS",
criteria: {
minTransactions: 12,
network: "test_network"
}
})
},
"admin"
)

const _stateId = await credentialsService.setOAuthState({
groupId: _groupId,
memberId: "123",
providerName: "blockchain"
})

const fun = credentialsService.addMember([_stateId], undefined, [
"0x"
])

await expect(fun).rejects.toThrow(`The network is not supported`)
})

it("Should add a member to a credential group", async () => {
const _stateId = await credentialsService.setOAuthState({
groupId,
Expand Down Expand Up @@ -398,5 +433,167 @@ describe("CredentialsService", () => {

expect(clientRedirectUri).toBeUndefined()
})

it("Should not add a member to a group with many credentials and return undefined if the OAuth state does not match the credential provider", async () => {
const { id: _groupId } = await groupsService.createGroup(
{
name: "Group4",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600,
credentials: JSON.stringify({
credentials: [
{
id: "BLOCKCHAIN_TRANSACTIONS",
criteria: {
minTransactions: 12,
network: "sepolia"
}
},
{
id: "GITHUB_FOLLOWERS",
criteria: {
minFollowers: 5
}
}
],
expression: ["", "and", ""]
})
},
"admin"
)

const _stateId1 = await credentialsService.setOAuthState({
groupId: _groupId,
memberId: "1",
providerName: "blockchain"
})

const _stateId2 = await credentialsService.setOAuthState({
groupId: _groupId,
memberId: "1",
providerName: "twitter"
})

const clientRedirectUri = await credentialsService.addMember(
[_stateId1, _stateId2],
["code"],
["0x"]
)

const group = await groupsService.getGroup(_groupId)

expect(clientRedirectUri).toBeUndefined()
expect(group.members).toHaveLength(0)
})

it("Should throw an error if the group with many credentials contains unsupported network", async () => {
const { id: _groupId } = await groupsService.createGroup(
{
name: "Group5",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600,
credentials: JSON.stringify({
credentials: [
{
id: "BLOCKCHAIN_TRANSACTIONS",
criteria: {
minTransactions: 12,
network: "test_network"
}
},
{
id: "GITHUB_FOLLOWERS",
criteria: {
minFollowers: 5
}
}
],
expression: ["", "and", ""]
})
},
"admin"
)

const _stateId1 = await credentialsService.setOAuthState({
groupId: _groupId,
memberId: "1",
providerName: "blockchain"
})

const _stateId2 = await credentialsService.setOAuthState({
groupId: _groupId,
memberId: "1",
providerName: "github"
})

const fun = credentialsService.addMember(
[_stateId1, _stateId2],
["code"],
["0x"]
)

await expect(fun).rejects.toThrow(`The network is not supported`)
})

it("Should throw an error if the OAuth account has already joined the group with many credentials", async () => {
const _stateId1 = await credentialsService.setOAuthState({
groupId,
memberId: "123",
providerName: "blockchain"
})

const _stateId2 = await credentialsService.setOAuthState({
groupId,
memberId: "123",
providerName: "github"
})

const fun = credentialsService.addMember(
[_stateId1, _stateId2],
["code"]
)

await expect(fun).rejects.toThrow(
`OAuth account has already joined the group`
)
})

it("Should throw an error if the OAuth account does not match the criteria of the group with many credentials", async () => {
;(getProvider as any).mockImplementation(
() =>
({
getAccessToken: jest.fn(() => "2"),
getProfile: jest.fn(() => ({
id: "id2"
}))
} as any)
)
;(validateManyCredentials as any).mockImplementationOnce(
async () => false
)

const _stateId1 = await credentialsService.setOAuthState({
groupId,
memberId: "124",
providerName: "blockchain"
})

const _stateId2 = await credentialsService.setOAuthState({
groupId,
memberId: "124",
providerName: "github"
})

const fun = credentialsService.addMember(
[_stateId1, _stateId2],
["code"]
)

await expect(fun).rejects.toThrow(
`OAuth account does not match criteria`
)
})
})
})
119 changes: 118 additions & 1 deletion apps/api/src/app/groups/groups.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe("GroupsService", () => {
id,
{
description: "This is a new description",
treeDepth: 16,
fingerprintDuration: 1000,
credentials: {
id: "TWITTER_FOLLOWERS",
Expand Down Expand Up @@ -266,7 +267,7 @@ describe("GroupsService", () => {
})
})

describe("# addMember", () => {
describe("# joinGroup", () => {
let invite: Invite

beforeAll(async () => {
Expand Down Expand Up @@ -309,6 +310,40 @@ describe("GroupsService", () => {
})
})

describe("# addMember", () => {
let _group: Group
const memberId = "123123"

beforeAll(async () => {
_group = await groupsService.createGroup(
{
name: "MemberGroup",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600
},
"admin"
)
})

it("Should add member to the group", async () => {
const { members } = await groupsService.addMember(
_group.id,
memberId
)

expect(members).toHaveLength(1)
})

it("Should not add member to a group if the member is already in the group", async () => {
const fun = groupsService.addMember(_group.id, memberId)

await expect(fun).rejects.toThrow(
`Member '${memberId}' is already in the group '${_group.name}'`
)
})
})

describe("# isGroupMember", () => {
it("Should return false if a member does not exist", () => {
const result = groupsService.isGroupMember(groupId, "123122")
Expand Down Expand Up @@ -1162,6 +1197,34 @@ describe("GroupsService", () => {
expect(members).toHaveLength(3)
})

it("Should not add a member to the group via API if the group is a credential group", async () => {
const _group = await groupsService.createGroup(
{
name: "Credential Group",
description: "This is a new group",
treeDepth: 16,
fingerprintDuration: 3600,
credentials: {
id: "GITHUB_FOLLOWERS",
criteria: {
minFollowers: 5
}
}
},
admin.id
)

const fun = groupsService.addMembersWithAPIKey(
_group.id,
["123123", "456456", "789789"],
apiKey
)

await expect(fun).rejects.toThrow(
`The group '${_group.name}' is a credential group. You cannot add members to a credential group using an API Key.`
)
})

it("Should not add a member if they already exist", async () => {
const fun = groupsService.addMembersWithAPIKey(
group.id,
Expand Down Expand Up @@ -1357,6 +1420,34 @@ describe("GroupsService", () => {
])
})

it("Should not add a member to the group if the group is a credential group", async () => {
const _group = await groupsService.createGroup(
{
name: "Credential Group",
description: "This is a new group",
treeDepth: 16,
fingerprintDuration: 3600,
credentials: {
id: "GITHUB_FOLLOWERS",
criteria: {
minFollowers: 5
}
}
},
"admin"
)

const fun = groupsService.addMembersManually(
_group.id,
["789789", "122121", "456456"],
"admin"
)

await expect(fun).rejects.toThrow(
`The group '${_group.name}' is a credential group. You cannot manually add members to a credential group.`
)
})

it("Should not add members if they already exists", async () => {
const fun = groupsService.addMembersManually(
group.id,
Expand Down Expand Up @@ -1776,6 +1867,32 @@ describe("GroupsService", () => {
})
})

describe("# fingerprint", () => {
it("Should get fingerprint", async () => {
const fingerprint = await groupsService.getFingerprint(groupId)

expect(fingerprint).toBeDefined()
})

it("Should get fingerprints", async () => {
const group = await groupsService.createGroup(
{
name: "Fingerprint Group",
description: "This is a description",
treeDepth: 16,
fingerprintDuration: 3600
},
"admin"
)
const fingerprints = await groupsService.getFingerprints([
groupId,
group.id
])

expect(fingerprints).toHaveLength(2)
})
})

describe("# initialize", () => {
it("Should initialize the cached groups", async () => {
const currentCachedGroups = await groupsService.getGroups()
Expand Down
Loading

0 comments on commit 9aa38db

Please sign in to comment.