Skip to content

Commit

Permalink
test: Integration tests for v1 (#103)
Browse files Browse the repository at this point in the history
  • Loading branch information
stanleyjones authored Apr 4, 2023
1 parent 28cf2f4 commit 1003b92
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 3 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
},
"scripts": {
"test": "jest",
"test:integration": "jest --testMatch '**/?(*.)+(integration).[jt]s?(x)'",
"build": "tsc",
"watch": "npm run clean && npm run build -- -w",
"clean": "rm -rf ./dist",
Expand All @@ -50,6 +51,10 @@
]
},
"jest": {
"collectCoverageFrom": [
"./src/**",
"!./src/**/__tests__/**"
],
"preset": "ts-jest",
"testEnvironment": "node",
"maxWorkers": 1,
Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Ed25519KeyPairIdentity } from "../identity"

export const SERVERS = {
LEDGER: "http://127.0.0.1:8000",
KEYVALUE: "http://127.0.0.1:8010",
}

export const ID1 = Ed25519KeyPairIdentity.fromPem(`
-----BEGIN PRIVATE KEY-----
MC4CAQAwBQYDK2VwBCIEIGgh+scXslP3zB4Jgkxtjf8vG60M9h4ZMjKg4RbxqWaG
-----END PRIVATE KEY-----
`)

export const ID_RND = () =>
Ed25519KeyPairIdentity.fromMnemonic(Ed25519KeyPairIdentity.getMnemonic())
68 changes: 68 additions & 0 deletions src/__tests__/network.integration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { Message } from "../message"
import { CborData } from "../message/cbor"
import { Base, Network } from "../network"
import { ID1, ID_RND, SERVERS } from "./data"

describe("Network", () => {
let network: Network
let HEX: string
let CBOR_DATA: CborData

beforeAll(() => {
const timestamp = Math.floor(Date.now() / 1000).toString(16)
HEX = `d28440a053d92711a2036673746174757305c11a${timestamp}40`
CBOR_DATA = Buffer.from(HEX, "hex")
})

it("should send encoded bytes", async () => {
network = new Network(SERVERS.LEDGER)
const status = await network.sendEncoded(CBOR_DATA)

expect(status).toBeDefined()
})
it("should send a request from bytes", async () => {
network = new Network(SERVERS.LEDGER)
const msg = Message.fromCborData(CBOR_DATA)
const status = await network.send(msg)

expect(status).toBeDefined()
})
it("should send a request from an object", async () => {
network = new Network(SERVERS.LEDGER)
const msg = Message.fromObject({ method: "status" })
const status = await network.send(msg)

expect(status).toBeDefined()
})
it("should call a method", async () => {
network = new Network(SERVERS.LEDGER)
network.apply([Base])
const status = await network.call("status")

expect(status).toBeDefined()
})
it("should call a method from a shortcut", async () => {
network = new Network(SERVERS.LEDGER)
network.apply([Base])

const status = await network.base.status()

expect(status).toBeDefined()
})
it("should call a method with a known identity", async () => {
network = new Network(SERVERS.LEDGER, ID1)
network.apply([Base])

const status = await network.base.status()

expect(status).toBeDefined()
})
it("should call a method with a random identity", async () => {
network = new Network(SERVERS.LEDGER, ID_RND())
network.apply([Base])

const status = await network.base.status()

expect(status).toBeDefined()
})
})
8 changes: 5 additions & 3 deletions src/message/cose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import cbor from "cbor";
import { sha3_224 } from "js-sha3";
import cbor from "cbor"
import { sha3_224 } from "js-sha3"
import {
Address,
AnonymousIdentity,
Expand Down Expand Up @@ -39,7 +39,9 @@ export class CoseMessage {

static fromCborData(data: CborData): CoseMessage {
const cose = cbor.decodeFirstSync(data, decoders).value
const protectedHeader = cbor.decodeFirstSync(cose[0])
const protectedHeader = cose[0]?.length
? cbor.decodeFirstSync(cose[0])
: cose[0]
const unprotectedHeader = cose[1]
const content = cbor.decodeFirstSync(cose[2], decoders).value
const signature = cose[3]
Expand Down

0 comments on commit 1003b92

Please sign in to comment.