1 | import chai, { expect } from "chai";
|
2 | import path from "path";
|
3 | import sinon from "sinon";
|
4 | import sinonChai from "sinon-chai";
|
5 | import fs from "fs";
|
6 |
|
7 | import JwtGenerator from "../lib/JwtGenerator";
|
8 |
|
9 | import Credentials from "../lib/Credentials";
|
10 |
|
11 | chai.use(sinonChai);
|
12 |
|
13 | describe("Credentials Object", function() {
|
14 | it("should be possible to construct a Credential object", function() {
|
15 | var cred = Credentials.parse("KEY", "SECRET");
|
16 |
|
17 | expect(cred).to.be.an.instanceof(Credentials);
|
18 | });
|
19 |
|
20 | it("should parse object literal into a Credential object", function() {
|
21 | var key = "KEY";
|
22 | var secret = "SECRET";
|
23 | var appId = "app-id";
|
24 | var privateKey = path.join(__dirname, "private-test.key");
|
25 | var obj = {
|
26 | apiKey: key,
|
27 | apiSecret: secret,
|
28 | applicationId: appId,
|
29 | privateKey: privateKey
|
30 | };
|
31 | var parsed = Credentials.parse(obj);
|
32 |
|
33 | expect(parsed).to.be.an.instanceof(Credentials);
|
34 | expect(parsed.apiKey).to.be.equal(key);
|
35 | expect(parsed.apiSecret).to.be.equal(secret);
|
36 | expect(parsed.applicationId).to.be.equal(appId);
|
37 | expect(parsed.privateKey).to.be.ok;
|
38 | });
|
39 |
|
40 | it("should throw an error when a privateKey is provided and the file does not exist", function() {
|
41 | var create = function() {
|
42 | return new Credentials("KEY", "SECRET", "./no-key-here.key");
|
43 | };
|
44 | expect(create).to.throw(Error);
|
45 | });
|
46 |
|
47 | it("should read a private key from a file into a Buffer accessible via Credentials.privateKey", function() {
|
48 | var cred = new Credentials(
|
49 | "KEY",
|
50 | "SECRET",
|
51 | path.join(__dirname, "private-test.key")
|
52 | );
|
53 | expect(cred.privateKey).to.be.an.instanceof(Buffer);
|
54 | });
|
55 |
|
56 | it("should turn a private key string into a Buffer accessible via Credentials.privateKey", function() {
|
57 | var key = fs.readFileSync(path.join(__dirname, "private-test.key"));
|
58 | var cred = new Credentials("KEY", "SECRET", key);
|
59 | expect(cred.privateKey).to.be.an.instanceof(Buffer);
|
60 | });
|
61 |
|
62 | it("should support passing a privateKey of type string", function() {
|
63 | var key = `-----BEGIN PRIVATE KEY-----
|
64 | blah blah blah
|
65 | -----END PRIVATE KEY-----`;
|
66 | var cred = new Credentials("KEY", "SECRET", key);
|
67 | expect(cred.privateKey).to.be.an.instanceof(Buffer);
|
68 | });
|
69 |
|
70 | it("should allow an applicationId to be provided upon construction", function() {
|
71 | var appId = "some_app_id";
|
72 | var cred = new Credentials(
|
73 | "KEY",
|
74 | "SECRET",
|
75 | path.join(__dirname, "private-test.key"),
|
76 | appId
|
77 | );
|
78 | expect(cred.applicationId).to.equal(appId);
|
79 | });
|
80 |
|
81 | it("should allow a JWT to be generated using supplied application ID", function() {
|
82 | var stub = sinon.createStubInstance(JwtGenerator);
|
83 |
|
84 | var cred = new Credentials(
|
85 | "KEY",
|
86 | "SECRET",
|
87 | path.join(__dirname, "private-test.key"),
|
88 | "app-id"
|
89 | );
|
90 | cred._setJwtGenerator(stub);
|
91 |
|
92 | cred.generateJwt();
|
93 |
|
94 | expect(stub.generate).to.be.calledWith(cred.privateKey, {
|
95 | application_id: cred.applicationId
|
96 | });
|
97 | });
|
98 |
|
99 | it("should allow a JWT to be generated using an alternative application ID", function() {
|
100 | var stub = sinon.createStubInstance(JwtGenerator);
|
101 |
|
102 | var cred = new Credentials(
|
103 | "KEY",
|
104 | "SECRET",
|
105 | path.join(__dirname, "private-test.key"),
|
106 | "app-id"
|
107 | );
|
108 | cred._setJwtGenerator(stub);
|
109 |
|
110 | var altAppId = "another-app-id";
|
111 | cred.generateJwt(altAppId);
|
112 |
|
113 | expect(stub.generate).to.be.calledWith(cred.privateKey, {
|
114 | application_id: altAppId
|
115 | });
|
116 | });
|
117 |
|
118 | it("should allow a JWT to be generated using an alternative private key", function() {
|
119 | var stub = sinon.createStubInstance(JwtGenerator);
|
120 |
|
121 | var cred = new Credentials(
|
122 | "KEY",
|
123 | "SECRET",
|
124 | path.join(__dirname, "private-test.key"),
|
125 | "app-id"
|
126 | );
|
127 | cred._setJwtGenerator(stub);
|
128 |
|
129 | var altAppId = "another-app-id";
|
130 | cred.generateJwt(altAppId, "ALTERNATIVE_KEY");
|
131 |
|
132 | expect(stub.generate).to.be.calledWith("ALTERNATIVE_KEY", {
|
133 | application_id: altAppId
|
134 | });
|
135 | });
|
136 | });
|