Skip to content
This repository has been archived by the owner on Apr 4, 2022. It is now read-only.

Commit

Permalink
fix tests for addresses and public-key.service
Browse files Browse the repository at this point in the history
  • Loading branch information
tboeckmann committed Aug 16, 2020
1 parent ddc42e6 commit 512ef11
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 7 deletions.
87 changes: 81 additions & 6 deletions src/app/services/mailchain/addresses/addresses.service.spec.ts
Expand Up @@ -7,6 +7,7 @@ import { HttpHelpersService } from '../../helpers/http-helpers/http-helpers.serv
import { ProtocolsServiceStub } from '../protocols/protocols.service.stub';
import { ProtocolsService } from '../protocols/protocols.service';
import { AddressesServiceStub } from './addresses.service.stub';
import { of } from 'rxjs';


describe('AddressesService', () => {
Expand All @@ -24,7 +25,7 @@ describe('AddressesService', () => {
providers: [
HttpHelpersService,
{ provide: ProtocolsService, useClass: ProtocolsServiceStub },
{ provide: AddressesService, useClass: AddressesServiceStub },
AddressesService,
],
imports: [HttpClientTestingModule]
});
Expand All @@ -43,20 +44,94 @@ describe('AddressesService', () => {


describe('initUrl', () => {
beforeEach(() => {
spyOn(addressesService, 'initUrl')
});
it('should initialize the url', () => {
expect(addressesService['url']).toEqual('http://127.0.0.1:8080/api')
expect(addressesService.initUrl).toHaveBeenCalled()
});
})

it('should be created', () => {
expect(addressesService).toBeTruthy();
});

it('should get an array of sender addresses', async () => {
let result
await addressesService.getAddresses('ethereum', 'mainnet').then(res => {
result = res
describe('getAddresses', () => {
beforeEach(() => {
spyOn(addressesService, 'getAddresses')
});
it('should get an array of sender addresses', async () => {
let result
await addressesService.getAddresses('ethereum', 'mainnet').then(res => {
result = res
});
expect(result).toEqual(expectedAddresses)
expect(addressesService.getAddresses).toHaveBeenCalled()
});
});
describe('handleAddressFormatting', () => {
it('should return address when "hex/0x-prefix" is specified', () => {
let address: string = mailchainTestService.senderAddressesHex0xPrefix[0]
let result: string = addressesService.handleAddressFormatting('hex/0x-prefix"', address)
expect(result).toEqual(address)
});
it('should return converted "hex/0x-prefix" as lowercase', () => {
let address: string = mailchainTestService.senderAddressesHex0xPrefix[0].toUpperCase()
let result: string = addressesService.handleAddressFormatting('hex/0x-prefix"', address)
expect(result).toEqual(address.toLowerCase())
});
it('should return address when "base58/plain" is specified', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormatting('base58/plain', address)
expect(result).toEqual(address)
});
it('should NOT return converted "base58/plain" as lowercase', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormatting('base58/plain', address)
expect(result).not.toEqual(address.toLowerCase())
});
it('should return address when an unknown format is specified', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormatting('base12/unknown', address)
expect(result).toEqual(address)
});
it('should NOT return converted unknown format as lowercase', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormatting('base12/unknown', address)
expect(result).not.toEqual(address.toLowerCase())
});
});
describe('handleAddressFormattingByProtocol', () => {
it('should return address when "ethereum" is specified', () => {
let address: string = mailchainTestService.senderAddressesHex0xPrefix[0]
let result: string = addressesService.handleAddressFormattingByProtocol('ethereum', address)
expect(result).toEqual(address)
});
it('should return converted "ethereum" as lowercase', () => {
let address: string = mailchainTestService.senderAddressesHex0xPrefix[0].toUpperCase()
let result: string = addressesService.handleAddressFormattingByProtocol('ethereum', address)
expect(result).toEqual(address.toLowerCase())
});
it('should return address when "substrate" is specified', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormattingByProtocol('substrate', address)
expect(result).toEqual(address)
});
it('should NOT return converted "substrate" as lowercase', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormattingByProtocol('substrate', address)
expect(result).not.toEqual(address.toLowerCase())
});
it('should return address when an unknown protocol is specified', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormattingByProtocol('unknown-protocol', address)
expect(result).toEqual(address)
});
it('should NOT return converted unknown protocol as lowercase', () => {
let address: string = mailchainTestService.senderAddressesBase58Plain[0]
let result: string = addressesService.handleAddressFormattingByProtocol('unknown-protocol', address)
expect(result).not.toEqual(address.toLowerCase())
});
expect(result).toEqual(expectedAddresses)
});
});
15 changes: 14 additions & 1 deletion src/app/services/mailchain/public-key/public-key.service.spec.ts
Expand Up @@ -7,57 +7,70 @@ import { ProtocolsService } from '../protocols/protocols.service';
import { ProtocolsServiceStub } from '../protocols/protocols.service.stub';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { PublicKeyServiceStub } from './public-key.service.stub';
import { of } from 'rxjs';
import { MailchainTestService } from 'src/app/test/test-helpers/mailchain-test.service';

describe('PublicKeyService', () => {
let publicKeyService: PublicKeyService;
let mailchainTestService: MailchainTestService

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
// PublicKeyService,
LocalStorageProtocolService,
HttpHelpersService,
PublicKeyService,
{ provide: ProtocolsService, useClass: ProtocolsServiceStub },
{ provide: PublicKeyService, useClass: PublicKeyServiceStub },
],
imports: [HttpClientTestingModule]
});

publicKeyService = TestBed.get(PublicKeyService);
mailchainTestService = TestBed.get(MailchainTestService);
});

it('should be created', () => {
expect(publicKeyService).toBeTruthy();
});

describe('getPublicKeyFromAddress', () => {
beforeEach(() => {
spyOn(publicKeyService, 'getPublicKeyFromAddress').and.returnValue(
of(mailchainTestService.publicKeyHexZeroXResponse())
)
});
it('should return `public-key`', async () => {
let result
await publicKeyService.getPublicKeyFromAddress("0x0123456789abcdef0123456789abcdef01234567", "ropsten").subscribe(res => {
result = res["body"]
})
expect(result["public-key"]).toBe("0x1234567890")
expect(publicKeyService.getPublicKeyFromAddress).toHaveBeenCalled()
});
it('should return `public-key-encoding`', async () => {
let result
await publicKeyService.getPublicKeyFromAddress("0x0123456789abcdef0123456789abcdef01234567", "ropsten").subscribe(res => {
result = res["body"]
})
expect(result["public-key-encoding"]).toBe("hex/0x-prefix")
expect(publicKeyService.getPublicKeyFromAddress).toHaveBeenCalled()
});
it('should return `public-key-kind`', async () => {
let result
await publicKeyService.getPublicKeyFromAddress("0x0123456789abcdef0123456789abcdef01234567", "ropsten").subscribe(res => {
result = res["body"]
})
expect(result["public-key-kind"]).toBe("secp256k1")
expect(publicKeyService.getPublicKeyFromAddress).toHaveBeenCalled()
});
it('should return `supported-encryption-types`', async () => {
let result
await publicKeyService.getPublicKeyFromAddress("0x0123456789abcdef0123456789abcdef01234567", "ropsten").subscribe(res => {
result = res["body"]
})
expect(result["supported-encryption-types"]).toEqual(["aes256cbc", "noop"])
expect(publicKeyService.getPublicKeyFromAddress).toHaveBeenCalled()
});

});
Expand Down
12 changes: 12 additions & 0 deletions src/app/test/test-helpers/mailchain-test.service.ts
Expand Up @@ -157,12 +157,24 @@ export class MailchainTestService {
};

public senderAddresses(): Array<any> {
// call senderAddressesEthereum
return this.senderAddressesHex0xPrefix()
}

public senderAddressesHex0xPrefix(): Array<any> {
return [
"0x92d8f10248c6a3953cc3692a894655ad05d61efb",
"0x0123456789012345678901234567890123456789"
]
}

public senderAddressesBase58Plain(): Array<any> {
return [
"5CaLgJUDdDRxw6KQXJY2f5hFkMEEGHvtUPQYDWdSbku42Dv2",
"5F4HMyes8GNWzpSDjTPSh61Aw6RTaWmZKwKvszocwqbsdn4h"
]
}

public senderAddressesEthereumObserveResponse() {
return {
"headers": {
Expand Down

0 comments on commit 512ef11

Please sign in to comment.