1 | import chai, { expect } from "chai";
|
2 | import path from "path";
|
3 | import sinon from "sinon";
|
4 | import sinonChai from "sinon-chai";
|
5 |
|
6 | import ResourceTestHelper from "./ResourceTestHelper";
|
7 |
|
8 | import CallsResource from "../lib/CallsResource";
|
9 | import StreamResource from "../lib/StreamResource";
|
10 | import TalkResource from "../lib/TalkResource";
|
11 | import DtmfResource from "../lib/DtmfResource";
|
12 | import HttpClient from "../lib/HttpClient";
|
13 | import Credentials from "../lib/Credentials";
|
14 |
|
15 | chai.use(sinonChai);
|
16 |
|
17 | var creds = Credentials.parse({
|
18 | applicationId: "some-id",
|
19 | privateKey: path.join(__dirname, "private-test.key")
|
20 | });
|
21 | var emptyCallback = () => {};
|
22 |
|
23 | describe("CallsResource", () => {
|
24 | var httpClientStub = null;
|
25 | var calls = null;
|
26 |
|
27 | beforeEach(() => {
|
28 | httpClientStub = sinon.createStubInstance(HttpClient);
|
29 | var options = {
|
30 | httpClient: httpClientStub
|
31 | };
|
32 | calls = new CallsResource(creds, options);
|
33 | });
|
34 |
|
35 | it("should allow a call to be created", () => {
|
36 | var params = {
|
37 | to: {
|
38 | type: "websocket",
|
39 | uri: "wss://example.com/socket",
|
40 | "content-type": "audio/l16;rate=16000",
|
41 | headers: {
|
42 | "utf-8": "✅"
|
43 | }
|
44 | }
|
45 | };
|
46 | calls.create(params, emptyCallback);
|
47 |
|
48 | var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(params, {
|
49 | method: "POST",
|
50 | headers: {
|
51 | "Content-Type": "application/json",
|
52 | "Content-Length": 124
|
53 | }
|
54 | });
|
55 | expect(httpClientStub.request).to.have.been.calledWith(
|
56 | sinon.match(expectedRequestArgs),
|
57 | emptyCallback
|
58 | );
|
59 | });
|
60 |
|
61 | it("should throw an error if no query is provided", () => {
|
62 | var expectThrow = function() {
|
63 | calls.get();
|
64 | };
|
65 |
|
66 | expect(expectThrow).to.throw(Error);
|
67 | });
|
68 |
|
69 | it("should get a collection of calls", () => {
|
70 | calls.get({}, emptyCallback);
|
71 |
|
72 | var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
|
73 | method: "GET",
|
74 | body: undefined,
|
75 | path: `${CallsResource.PATH}`
|
76 | });
|
77 |
|
78 | expect(httpClientStub.request).to.have.been.calledWith(
|
79 | sinon.match(expectedRequestArgs),
|
80 | emptyCallback
|
81 | );
|
82 | });
|
83 |
|
84 | it("should get a single of call using a call ID", () => {
|
85 | const callId = "2342342-lkjhlkjh-32423";
|
86 | calls.get(callId, emptyCallback);
|
87 |
|
88 | var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
|
89 | method: "GET",
|
90 | body: undefined,
|
91 | path: `${CallsResource.PATH}/${callId}`
|
92 | });
|
93 |
|
94 | expect(httpClientStub.request).to.have.been.calledWith(
|
95 | sinon.match(expectedRequestArgs),
|
96 | emptyCallback
|
97 | );
|
98 | });
|
99 |
|
100 | it("should get a allow calls to be queried by filter", () => {
|
101 | calls.get(
|
102 | {
|
103 | status: "answered"
|
104 | },
|
105 | emptyCallback
|
106 | );
|
107 |
|
108 | var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(null, {
|
109 | method: "GET",
|
110 | body: undefined,
|
111 | path: `${CallsResource.PATH}?status=answered`
|
112 | });
|
113 |
|
114 | expect(httpClientStub.request).to.have.been.calledWith(
|
115 | sinon.match(expectedRequestArgs),
|
116 | emptyCallback
|
117 | );
|
118 | });
|
119 |
|
120 | it("should allow a call to be updated", () => {
|
121 | const callId = "2342342-lkjhlkjh-32423";
|
122 | var params = {
|
123 | action: "hangup",
|
124 | destination: {
|
125 | type: "ncco",
|
126 | url: ["http://exémple.com/ncco.json"]
|
127 | }
|
128 | };
|
129 | calls.update(callId, params, emptyCallback);
|
130 |
|
131 | var expectedRequestArgs = ResourceTestHelper.requestArgsMatch(params, {
|
132 | method: "PUT",
|
133 | path: `${CallsResource.PATH}/${callId}`,
|
134 | headers: {
|
135 | "Content-Type": "application/json",
|
136 | "Content-Length": 89
|
137 | }
|
138 | });
|
139 |
|
140 | expect(httpClientStub.request).to.have.been.calledWith(
|
141 | sinon.match(expectedRequestArgs),
|
142 | emptyCallback
|
143 | );
|
144 | });
|
145 |
|
146 | it("should expose a stream property", () => {
|
147 | expect(calls.stream).to.be.an.instanceOf(StreamResource);
|
148 | });
|
149 |
|
150 | it("should expose a talk property", () => {
|
151 | expect(calls.talk).to.be.an.instanceOf(TalkResource);
|
152 | });
|
153 |
|
154 | it("should expose a dtmf property", () => {
|
155 | expect(calls.dtmf).to.be.an.instanceOf(DtmfResource);
|
156 | });
|
157 | });
|