-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.test.js
76 lines (52 loc) · 2.76 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const {checkHmacValidity, checkWebhookHmacValidity, createRawBody} = require('./index')
const chai = require('chai')
const mockKey = "YYYYYYYYYY"
const mockObj = {
something: "something",
hmac: "a4a1af98de6ff6865728c384aee46377ede29e31237a02620f3a346d3005e975",
more: "else",
}
const stringQueryValid = "?hmac=a4a1af98de6ff6865728c384aee46377ede29e31237a02620f3a346d3005e975&more=else&something=something"
const stringQueryInvalid = "?hmac=a4a1af98de6ff6865728c384aee46377ede29e31237a02620f3a3465e975&more=else&something=something"
const mockObjInvalid = {
something: "something",
hmac: "09851bfdff804ad33223dd2622ec",
something: "else",
}
describe("Install: It validates the hmac against the hash of the QS passed in", () => {
it("Passing in a valid mock it will return true", () => {
chai.expect(checkHmacValidity(mockKey, mockObj)).to.be.true
})
it("Passing in an invalid mock it will return false", () => {
chai.expect(checkHmacValidity(mockKey, mockObjInvalid)).to.be.false
})
it("Passing a string will have it expanded and validated", () => {
chai.expect(checkHmacValidity(mockKey, stringQueryValid)).to.be.true
})
it("Passing a string will have it expanded and invalidated", () => {
chai.expect(checkHmacValidity(mockKey, stringQueryInvalid)).to.be.false
})
})
const mockBuffer64 = "eyJzaG9wX2lkIjoyNjk5OTI1OTIxNSwic2hvcF9kb21haW4iOiJzdG9yZWZyb250LWV4cGVyaWVuY2UubXlzaG9waWZ5LmNvbSIsImN1c3RvbWVyIjp7ImlkIjoyOTg3OTU2NzY0NzUxLCJlbWFpbCI6ImxlaWdoYnJlbmRvbmJhcm5lcysxOUBnbWFpbC5jb20iLCJwaG9uZSI6bnVsbH0sIm9yZGVyc19yZXF1ZXN0ZWQiOltdfQ=="
const payload = Buffer.from(mockBuffer64, 'base64').toString()
describe("Webhook: It validates the hmac against the hash of the rawBody and secret passed in", () => {
it("Passing in a valid mock it will return true", () => {
chai.expect(checkWebhookHmacValidity(mockKey, JSON.stringify(payload), '2esWad2rPMt/alOK9Wfm9bbG1fvPmOHbmwQg+MmXDLo=')).to.be.true
})
it("Passing in an invalid payload it will return false", () => {
chai.expect(checkWebhookHmacValidity(mockKey, JSON.stringify({}), '2esWad2rPMt/alOK9Wfm9bbG1fvPmOHbmwQg+MmXDLo=')).to.be.false
})
it("Passing in an invalid secret payload it will return false", () => {
chai.expect(checkWebhookHmacValidity('', JSON.stringify(payload), '2esWad2rPMt/alOK9Wfm9bbG1fvPmOHbmwQg+MmXDLo=')).to.be.false
})
it("Passing in an missing params should fail it", () => {
chai.expect(checkWebhookHmacValidity(null, null, '2esWad2rPMt/alOK9Wfm9bbG1fvPmOHbmwQg+MmXDLo=')).to.be.false
})
})
const mockReqBody = {something: 'someValue'}
const equateTo = JSON.stringify(mockReqBody)
describe("createRawBody: Return a string from a buffer", () => {
it("Passing in a valid mock it will return true", () => {
chai.expect(createRawBody({something: "someValue"})).to.be.eql(equateTo)
})
})