Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds tests for encrypt stream #52

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/streams/encryptStream.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default class EncryptStream extends Transform {
this.key = bytesFromHandle(handle);
this.genesisHash = Datamap.genesisHash(handle);
}
_transform(chunk, encoding, callback) {
_transform(chunk, _encoding, callback) {
const key = this.key;
const iv = deriveNonce(this.key, chunk.idx);

Expand Down
52 changes: 52 additions & 0 deletions src/streams/encryptStream.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { Transform } from "readable-stream";
import Datamap from "datamap-generator";

import EncryptStream from "./encryptStream";
import { bytesFromHandle } from "../util";

test("EncryptStream extends Transform", () => {
const handle = "foobar";
const options = { foo: "bar" };
const encryptStream = new EncryptStream(handle, options);
expect(encryptStream).toBeInstanceOf(Transform);
});

test("constructor", () => {
const handle = "foobar";
const options = { foo: "bar" };
const encryptStream = new EncryptStream(handle, options);

expect(encryptStream.options).toEqual({ objectMode: true, ...options });
expect(encryptStream.key).toEqual(bytesFromHandle(handle));
expect(encryptStream.genesisHash).toEqual(Datamap.genesisHash(handle));
});

test("_transform invokes the callback", () => {
const chunk = { idx: 1 };
const callback = jest.fn();

const handle = "foobar";
const options = { foo: "bar" };
const encryptStream = new EncryptStream(handle, options);

encryptStream._transform(chunk, "utf-8", callback);

expect(callback).toHaveBeenCalled();
});

test("_transform invokes the callback", () => {
const chunk = { idx: 1 };
const callback = jest.fn();

const handle = "foobar";
const options = { foo: "bar" };
const encryptStream = new EncryptStream(handle, options);

encryptStream._transform(chunk, "utf-8", callback);

const expectedChunk = {
data: "E9UBOHMCD9LEIBCES9JEFGZEIDJIF9CFHCWGQBOCMEYCJIB9M9L9KGSFFELFLINHA",
idx: 1
};
expect(callback).toHaveBeenCalledWith(null, expectedChunk);
});