Skip to content

Commit

Permalink
feat(client): removeAccounts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasGassmann committed May 25, 2020
1 parent 7879d09 commit f0cb845
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,6 @@ jspm_packages/

dist/

.history
.history

storage.json
14 changes: 13 additions & 1 deletion src/managers/AccountManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,19 @@ export class AccountManager {
const accounts = await this.storage.get(StorageKey.ACCOUNTS)

const filteredAccounts = accounts.filter(
(accountInfo) => accountInfo.accountIdentifier !== accountIdentifier
(account) => account.accountIdentifier !== accountIdentifier
)

return this.storage.set(StorageKey.ACCOUNTS, filteredAccounts)
}

public async removeAccounts(accountIdentifiers: string[]): Promise<void> {
const accounts = await this.storage.get(StorageKey.ACCOUNTS)

const filteredAccounts = accounts.filter((account) =>
accountIdentifiers.every(
(accountIdentifier) => account.accountIdentifier !== accountIdentifier
)
)

return this.storage.set(StorageKey.ACCOUNTS, filteredAccounts)
Expand Down
12 changes: 12 additions & 0 deletions src/managers/PermissionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ export class PermissionManager {
return this.storage.set(StorageKey.PERMISSION_LIST, filteredPermissions)
}

public async removePermissions(accountIdentifiers: string[]): Promise<void> {
const permissions: PermissionInfo[] = await this.storage.get(StorageKey.PERMISSION_LIST)

const filteredPermissions = permissions.filter((permission) =>
accountIdentifiers.every(
(accountIdentifier) => permission.accountIdentifier !== accountIdentifier
)
)

return this.storage.set(StorageKey.PERMISSION_LIST, filteredPermissions)
}

public async removeAllPermissions(): Promise<void> {
return this.storage.delete(StorageKey.PERMISSION_LIST)
}
Expand Down
158 changes: 158 additions & 0 deletions test/managers/AccountManager.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import 'mocha'
import { AccountManager } from '../../src/managers/AccountManager'

import { AccountInfo, Origin, NetworkType } from '../../src'
import { FileStorage, writeLocalFile } from '../test-utils/FileStorage'

// use chai-as-promised plugin
chai.use(chaiAsPromised)
const expect = chai.expect

// TODO: Use AccountInfo type
const account1: any = {
accountIdentifier: 'a1',
beaconId: 'id1',
origin: {
type: Origin.P2P,
id: 'o1'
},
address: 'tz1',
pubkey: 'pubkey1',
network: { type: NetworkType.MAINNET },
scopes: [],
connectedAt: new Date().getTime()
}

const account2: any = {
accountIdentifier: 'a2',
beaconId: 'id2',
origin: {
type: Origin.P2P,
id: 'o2'
},
address: 'tz2',
pubkey: 'pubkey2',
network: { type: NetworkType.MAINNET },
scopes: [],
connectedAt: new Date().getTime()
}

const account3: any = {
accountIdentifier: 'a3',
beaconId: 'id3',
origin: {
type: Origin.P2P,
id: 'o3'
},
address: 'tz3',
pubkey: 'pubkey3',
network: { type: NetworkType.MAINNET },
scopes: [],
connectedAt: new Date().getTime()
}

describe(`AccountManager`, () => {
let manager: AccountManager
beforeEach(async () => {
await writeLocalFile({})

manager = new AccountManager(new FileStorage())
})
it(`reads and adds accounts`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
const accountsAfter: AccountInfo[] = await manager.getAccounts()

expect(accountsAfter.length, 'after').to.equal(1)
})

it(`reads and adds multiple accounts`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
await manager.addAccount(account2)
const accountsAfter: AccountInfo[] = await manager.getAccounts()

expect(accountsAfter.length, 'after').to.equal(2)
})

it(`only adds an account once`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
await manager.addAccount(account1)
const accountsAfter: AccountInfo[] = await manager.getAccounts()

expect(accountsAfter.length, 'after').to.equal(1)
})

it(`reads one account`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
await manager.addAccount(account2)
const account = await manager.getAccount(account1.accountIdentifier)
expect(account, 'after').to.deep.include(account1)
})

it(`removes one account`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
await manager.addAccount(account2)
await manager.addAccount(account3)
const accountsAfter: AccountInfo[] = await manager.getAccounts()

expect(accountsAfter.length, 'after add').to.equal(3)

await manager.removeAccount(account1.accountIdentifier)
const accountsAfterRemove: AccountInfo[] = await manager.getAccounts()

expect(accountsAfterRemove.length, 'after remove').to.equal(2)
expect(accountsAfterRemove, 'after remove, account2').to.deep.include(account2)
expect(accountsAfterRemove, 'after remove, account3').to.deep.include(account3)
})

it(`removes many accounts`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
await manager.addAccount(account2)
await manager.addAccount(account3)
const accountsAfter: AccountInfo[] = await manager.getAccounts()

expect(accountsAfter.length, 'after add').to.equal(3)

await manager.removeAccounts([account1.accountIdentifier, account2.accountIdentifier])
const accountsAfterRemove: AccountInfo[] = await manager.getAccounts()

expect(accountsAfterRemove.length, 'after remove').to.equal(1)
expect(accountsAfterRemove, 'after remove').to.deep.include(account3)
})

it(`removes all accounts`, async () => {
const accountsBefore: AccountInfo[] = await manager.getAccounts()
expect(accountsBefore.length, 'before').to.equal(0)

await manager.addAccount(account1)
await manager.addAccount(account2)
await manager.addAccount(account3)
const accountsAfter: AccountInfo[] = await manager.getAccounts()

expect(accountsAfter.length, 'after add').to.equal(3)

await manager.removeAllAccounts()
const accountsAfterRemove: AccountInfo[] = await manager.getAccounts()

expect(accountsAfterRemove.length, 'after remove').to.equal(0)
})
})
72 changes: 72 additions & 0 deletions test/test-utils/FileStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { readFile, writeFile } from 'fs'
import { defaultValues } from '../../src/types/storage/StorageKeyReturnDefaults'
import { Storage, StorageKey, StorageKeyReturnType } from '../..'

const file: string = './storage.json'

interface JsonObject {
[key: string]: unknown
}

/* eslint-disable prefer-arrow/prefer-arrow-functions */

export function readLocalFile(): Promise<JsonObject> {
return new Promise((resolve: (_: JsonObject) => void, reject: (error: unknown) => void): void => {
readFile(file, { encoding: 'utf8' }, (fileReadError: unknown, fileContent: string) => {
if (fileReadError) {
reject(fileReadError)
}
try {
const json: JsonObject = JSON.parse(fileContent)
resolve(json)
} catch (jsonParseError) {
reject(jsonParseError)
}
})
})
}

export function writeLocalFile(json: JsonObject): Promise<void> {
return new Promise((resolve: (_: void) => void): void => {
const fileContent: string = JSON.stringify(json)
writeFile(file, fileContent, { encoding: 'utf8' }, () => {
resolve()
})
})
}

/**
* This can be used for development in node.
*
* DO NOT USE IN PRODUCTION
*/
export class FileStorage implements Storage {
public static async isSupported(): Promise<boolean> {
return Promise.resolve(typeof global !== 'undefined')
}

public async get<K extends StorageKey>(key: K): Promise<StorageKeyReturnType[K]> {
const json: JsonObject = await readLocalFile()

if (json[key]) {
return json[key] as StorageKeyReturnType[K]
} else {
return JSON.parse(JSON.stringify(defaultValues[key]))
}
}

public async set<K extends StorageKey>(key: K, value: StorageKeyReturnType[K]): Promise<void> {
const json: JsonObject = await readLocalFile()

json[key] = value

return writeLocalFile(json)
}

public async delete<K extends StorageKey>(key: K): Promise<void> {
const json: JsonObject = await readLocalFile()
json[key] = undefined

return writeLocalFile(json)
}
}

0 comments on commit f0cb845

Please sign in to comment.