|
| 1 | +var lib = require("../"); |
| 2 | +var helper = require("./helper"); |
| 3 | +var nock = require("nock"); |
| 4 | +var Application = lib.Application; |
| 5 | + |
| 6 | +describe("Application", function(){ |
| 7 | + before(function(){ |
| 8 | + nock.disableNetConnect(); |
| 9 | + helper.setupGlobalOptions(); |
| 10 | + }); |
| 11 | + after(function(){ |
| 12 | + nock.cleanAll(); |
| 13 | + nock.enableNetConnect(); |
| 14 | + }); |
| 15 | + describe("#list", function(){ |
| 16 | + it("should return list of applications", function(done){ |
| 17 | + var span = helper.nock().get("/accounts/FakeAccountId/applications").reply(200, helper.xml.applications, {"Content-Type": "application/xml"}); |
| 18 | + Application.list(helper.createClient(), function(err, list){ |
| 19 | + if(err){ |
| 20 | + return done(err); |
| 21 | + } |
| 22 | + span.isDone().should.be.true; |
| 23 | + list.length.should.equal(2); |
| 24 | + list[0].applicationId.should.equal(1); |
| 25 | + list[0].appName.should.equal("Test Application"); |
| 26 | + list[0].serviceType.should.equal("Messaging-V2"); |
| 27 | + list[0].msgCallbackUrl.should.equal("http://a.com"); |
| 28 | + done(); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + it("should return list of applications (with default client)", function(done){ |
| 33 | + var span = helper.nock().get("/accounts/FakeAccountId/applications").reply(200, helper.xml.applications, {"Content-Type": "application/xml"}); |
| 34 | + Application.list(function(err, list){ |
| 35 | + if(err){ |
| 36 | + return done(err); |
| 37 | + } |
| 38 | + span.isDone().should.be.true; |
| 39 | + list.length.should.equal(2); |
| 40 | + list[0].applicationId.should.equal(1); |
| 41 | + list[0].appName.should.equal("Test Application"); |
| 42 | + list[0].serviceType.should.equal("Messaging-V2"); |
| 43 | + list[0].msgCallbackUrl.should.equal("http://a.com"); |
| 44 | + done(); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + it("should fail for error status code", function(done){ |
| 49 | + helper.nock().get("/accounts/FakeAccountId/applications").reply(400); |
| 50 | + Application.list(helper.createClient(), function(err, list){ |
| 51 | + if(err){ |
| 52 | + return done(); |
| 53 | + } |
| 54 | + done(new Error("An error is estimated")); |
| 55 | + }); |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + describe("#get", function(){ |
| 60 | + it("should return an application", function(done){ |
| 61 | + var span = helper.nock().get("/accounts/FakeAccountId/applications/1").reply(200, helper.xml.application, {"Content-Type": "application/xml"}); |
| 62 | + Application.get(helper.createClient(), "1", function(err, item){ |
| 63 | + if(err){ |
| 64 | + return done(err); |
| 65 | + } |
| 66 | + span.isDone().should.be.true; |
| 67 | + item.applicationId.should.equal(1); |
| 68 | + item.appName.should.equal("Test Application"); |
| 69 | + item.serviceType.should.equal("Messaging-V2"); |
| 70 | + item.msgCallbackUrl.should.equal("http://a.com"); |
| 71 | + done(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + it("should return an application (with default client)", function(done){ |
| 75 | + var span = helper.nock().get("/accounts/FakeAccountId/applications/1").reply(200, helper.xml.application, {"Content-Type": "application/xml"}); |
| 76 | + Application.get("1", function(err, item){ |
| 77 | + if(err){ |
| 78 | + return done(err); |
| 79 | + } |
| 80 | + span.isDone().should.be.true; |
| 81 | + item.applicationId.should.equal(1); |
| 82 | + item.appName.should.equal("Test Application"); |
| 83 | + item.serviceType.should.equal("Messaging-V2"); |
| 84 | + item.msgCallbackUrl.should.equal("http://a.com"); |
| 85 | + done(); |
| 86 | + }); |
| 87 | + }); |
| 88 | + it("should fail for error status code", function(done){ |
| 89 | + var span = helper.nock().get("/accounts/FakeAccountId/applications/1").reply(400); |
| 90 | + Application.get(helper.createClient(), "1", function(err, item){ |
| 91 | + if(err){ |
| 92 | + return done(); |
| 93 | + } |
| 94 | + done(new Error("An error is estimated")); |
| 95 | + }); |
| 96 | + }); |
| 97 | + }); |
| 98 | + describe("#create", function(){ |
| 99 | + it("should create a messaging application", function(done){ |
| 100 | + var data = {appName: "Test Application", serviceType: "Messaging-V2"}; |
| 101 | + helper.nock().post("/accounts/FakeAccountId/applications", helper.buildXml({application: data})).reply(201, "", {"Location": "/accounts/FakeAccountId/applications/1"}); |
| 102 | + helper.nock().get("/accounts/FakeAccountId/applications/1").reply(200, helper.xml.application, {"Content-Type": "application/xml"}); |
| 103 | + Application.createMessagingApplication(helper.createClient(), data, function(err, item){ |
| 104 | + if(err){ |
| 105 | + return done(err); |
| 106 | + } |
| 107 | + item.applicationId.should.equal(1); |
| 108 | + item.appName.should.equal("Test Application"); |
| 109 | + item.serviceType.should.equal("Messaging-V2"); |
| 110 | + done(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + it("should create a messaging application (with default client)", function(done){ |
| 114 | + var data = {appName: "Test Application", serviceType: "Messaging-V2"}; |
| 115 | + helper.nock().post("/accounts/FakeAccountId/applications", helper.buildXml({application: data})).reply(201, "", {"Location": "/accounts/FakeAccountId/applications/1"}); |
| 116 | + helper.nock().get("/accounts/FakeAccountId/applications/1").reply(200, helper.xml.application, {"Content-Type": "application/xml"}); |
| 117 | + Application.createMessagingApplication(data, function(err, item){ |
| 118 | + if(err){ |
| 119 | + return done(err); |
| 120 | + } |
| 121 | + item.applicationId.should.equal(1); |
| 122 | + item.appName.should.equal("Test Application"); |
| 123 | + item.serviceType.should.equal("Messaging-V2"); |
| 124 | + done(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + it("should create a voice application", function(done){ |
| 128 | + var sendData = {appName: "Test Application"}; |
| 129 | + var receivedData = {appName: "Test Application", serviceType: "Voice-V2"} |
| 130 | + helper.nock().post("/accounts/FakeAccountId/applications", helper.buildXml({application: receivedData})).reply(201, "", {"Location": "/accounts/FakeAccountId/applications/1"}); |
| 131 | + helper.nock().get("/accounts/FakeAccountId/applications/1").reply(200, helper.xml.voiceApplication, {"Content-Type": "application/xml"}); |
| 132 | + Application.createVoiceApplication(helper.createClient(), sendData, function(err, item){ |
| 133 | + if(err){ |
| 134 | + return done(err); |
| 135 | + } |
| 136 | + item.applicationId.should.equal(2); |
| 137 | + item.appName.should.equal("Test Application 2"); |
| 138 | + console.log(item) |
| 139 | + item.serviceType.should.equal("Voice-V2"); |
| 140 | + done(); |
| 141 | + }); |
| 142 | + }); |
| 143 | + it("should create a voice application (with default client)", function(done){ |
| 144 | + var sendData = {appName: "Test Application"}; |
| 145 | + var receivedData = {appName: "Test Application", serviceType: "Voice-V2"} |
| 146 | + helper.nock().post("/accounts/FakeAccountId/applications", helper.buildXml({application: receivedData})).reply(201, "", {"Location": "/accounts/FakeAccountId/applications/1"}); |
| 147 | + helper.nock().get("/accounts/FakeAccountId/applications/1").reply(200, helper.xml.voiceApplication, {"Content-Type": "application/xml"}); |
| 148 | + Application.createVoiceApplication(sendData, function(err, item){ |
| 149 | + if(err){ |
| 150 | + return done(err); |
| 151 | + } |
| 152 | + item.applicationId.should.equal(2); |
| 153 | + item.appName.should.equal("Test Application 2"); |
| 154 | + console.log(item) |
| 155 | + item.serviceType.should.equal("Voice-V2"); |
| 156 | + done(); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + it("should fail on error status code", function(done){ |
| 161 | + var data = {appName: "Test Application", msgCallbackUrl: "http://example.com"}; |
| 162 | + helper.nock().post("/accounts/FakeAccountId/applications").reply(400, ""); |
| 163 | + Application.createMessagingApplication(helper.createClient(), data, function(err, item){ |
| 164 | + if(err){ |
| 165 | + return done(); |
| 166 | + } |
| 167 | + done(new Error("An error is expected")); |
| 168 | + }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + describe("#update", function(){ |
| 172 | + it("should update an application", function(done){ |
| 173 | + var data = {appName: "Test Application"}; |
| 174 | + helper.nock().put("/accounts/FakeAccountId/applications/1", helper.buildXml({application: data})).reply(200); |
| 175 | + var app = new Application(); |
| 176 | + app.applicationId = 1; |
| 177 | + app.client = helper.createClient(); |
| 178 | + app.update(data, done); |
| 179 | + }); |
| 180 | + }); |
| 181 | + describe("#delete", function(){ |
| 182 | + it("should delete an application", function(done){ |
| 183 | + helper.nock().delete("/accounts/FakeAccountId/applications/1").reply(200); |
| 184 | + var app = new Application(); |
| 185 | + app.applicationId = 1; |
| 186 | + app.client = helper.createClient(); |
| 187 | + app.delete(done); |
| 188 | + }); |
| 189 | + }); |
| 190 | + describe("#getSipPeers", function(){ |
| 191 | + it("should return list of sippeers associated with the application", function(done){ |
| 192 | + var span = helper.nock().get("/accounts/FakeAccountId/applications/1/associatedsippeers").reply(200, helper.xml.applicationSipPeers, {"Content-Type": "application/xml"}); |
| 193 | + var app = new Application(); |
| 194 | + app.client = helper.createClient(); |
| 195 | + app.applicationId = "1"; |
| 196 | + app.getSipPeers(function(err, list){ |
| 197 | + if(err){ |
| 198 | + return done(err); |
| 199 | + } |
| 200 | + span.isDone().should.be.true; |
| 201 | + list.length.should.equal(1); |
| 202 | + list[0].siteId.should.equal(1); |
| 203 | + list[0].siteName.should.equal("Site Name"); |
| 204 | + list[0].peerName.should.equal("Peer Name"); |
| 205 | + list[0].peerId.should.equal(2) |
| 206 | + done(); |
| 207 | + }); |
| 208 | + }); |
| 209 | + it("should fail on error status code", function(done){ |
| 210 | + helper.nock().get("/accounts/FakeAccountId/applications/1/associatedsippeers").reply(400); |
| 211 | + var app = new Application(); |
| 212 | + app.client = helper.createClient(); |
| 213 | + app.id = "1"; |
| 214 | + app.getSipPeers(function(err, list){ |
| 215 | + if(err){ |
| 216 | + return done(); |
| 217 | + } |
| 218 | + done(new Error("An error is expected")); |
| 219 | + }); |
| 220 | + }); |
| 221 | + }); |
| 222 | + |
| 223 | + |
| 224 | +}); |
0 commit comments