diff --git a/src/1_utils.js b/src/1_utils.js index 38050e7c..544885ca 100644 --- a/src/1_utils.js +++ b/src/1_utils.js @@ -201,7 +201,8 @@ utils.messages = { blockedByClient: 'Request blocked by client, probably adblock', missingUrl: 'Required argument: URL, is missing', trackingDisabled: 'Requested operation cannot be completed since tracking is disabled', - deepviewNotCalled: 'Cannot call Deepview CTA, please call branch.deepview() first' + deepviewNotCalled: 'Cannot call Deepview CTA, please call branch.deepview() first', + missingIdentity: 'setIdentity - required argument identity should have a non-null value' }; /** diff --git a/src/2_resources.js b/src/2_resources.js index f2d0dd1a..94efb07e 100644 --- a/src/2_resources.js +++ b/src/2_resources.js @@ -145,16 +145,6 @@ resources.logout = { }) }; -resources.profile = { - destination: config.api_endpoint, - endpoint: "/v1/profile", - method: utils.httpMethod.POST, - params: defaults({ - "identity_id": validator(true, validationTypes.STRING), - "identity": validator(true, validationTypes.STRING) - }) -}; - resources.link = { destination: config.api_endpoint, endpoint: "/v1/url", diff --git a/src/6_branch.js b/src/6_branch.js index eeb398fc..35940eeb 100644 --- a/src/6_branch.js +++ b/src/6_branch.js @@ -785,32 +785,22 @@ Branch.prototype['first'] = wrap(callback_params.CALLBACK_ERR_DATA, function(don /*** +TOC_ITEM #setidentityidentity-callback &.setIdentity()& ^ALL ***/ Branch.prototype['setIdentity'] = wrap(callback_params.CALLBACK_ERR_DATA, function(done, identity) { var self = this; - this._api( - resources.profile, - { - "identity": identity - }, - function(err, data) { - if (err) { - done(err); - } - - data = data || { }; - self.identity_id = data['identity_id'] ? data['identity_id'].toString() : null; - self.sessionLink = data['link']; - - self.identity = identity; - data['developer_identity'] = identity; - - data['referring_data_parsed'] = data['referring_data'] ? - safejson.parse(data['referring_data']) : - null; + if (identity) { + var data = { + identity_id: self.identity_id, + session_id: self.session_id, + link: self.sessionLink, + developer_identity: identity + }; + self.identity = identity; + // store the identity + session.patch(self._storage, { "identity": identity }, true); + done(null, data); - // /v1/profile will return a new identity_id, but the same session_id - session.patch(self._storage, { "identity": identity, "identity_id": self.identity_id }, true); - done(null, data); - } - ); + } + else { + done(new Error(utils.message(utils.messages.missingIdentity))); + } }); /** diff --git a/test/1_utils.js b/test/1_utils.js index 9203d25b..b03041db 100644 --- a/test/1_utils.js +++ b/test/1_utils.js @@ -301,6 +301,14 @@ describe('utils', function() { 'Expected Missing url message produced' ); }); + + it('should produce a missing identity error', function() { + assert.strictEqual( + utils.message(utils.messages.missingIdentity), + 'setIdentity - required argument identity should have a non-null value', + 'Expected Missing identity message produced' + ); + }); }); describe('getParamValue', function() { diff --git a/test/3_api.js b/test/3_api.js index 212d30db..013c63f2 100644 --- a/test/3_api.js +++ b/test/3_api.js @@ -142,119 +142,6 @@ describe('Server', function() { } }); - describe('XHR Request', function() { - beforeEach(function() { - requests = []; - storage.clear(); - }); - - it('should instantiate an XHR', function(done) { - var assert = testUtils.plan(4, done); - var spyCallback = sinon.spy(); - server.XHRRequest( - resources.profile, - testUtils.params({ "identity": "test_id" }), - 'POST', - storage, - spyCallback - ); - assert.strictEqual(requests.length, 1, 'Request made'); - assert.strictEqual(requests[0].timeout, 5000, 'timeout set to 5s'); - assert.strictEqual( - requests[0].requestHeaders['Content-Type'], - 'application/x-www-form-urlencoded;charset=utf-8' - ); - requests[0].ontimeout(); - assert(spyCallback.calledOnce); - }); - - it('should succeed on a status=200', function(done) { - var assert = testUtils.plan(1, done); - var responseText = 'response'; - server.XHRRequest( - resources.profile, - testUtils.params({ "identity": "test_id" }), - 'POST', - storage, - function(err, data) { - assert.strictEqual( - Object.getOwnPropertyNames(data).length, - 0, - 'successful response' - ); - } - ); - requests[0].status = 200; - requests[0].readyState = 4; - requests[0].responseText = responseText; - requests[0].onreadystatechange(); - }); - - it('should error on a status=500', function(done) { - var assert = testUtils.plan(1, done); - var responseText = 'response'; - server.XHRRequest( - resources.profile, - testUtils.params({ "identity": "test_id" }), - 'POST', - storage, - function(err) { - assert.strictEqual( - err.message, - 'Error in API: ' + requests[0].status, - 'correct error message' - ); - } - ); - requests[0].status = 500; - requests[0].readyState = 4; - requests[0].responseText = responseText; - requests[0].onreadystatechange(); - }); - - it('should error on a status=400', function(done) { - var assert = testUtils.plan(1, done); - var responseText = 'response'; - server.XHRRequest( - resources.profile, - testUtils.params({ "identity": "test_id" }), - 'POST', - storage, - function(err) { - assert.strictEqual( - err.message, - 'Error in API: ' + requests[0].status, - 'correct error message' - ); - } - ); - requests[0].status = 400; - requests[0].readyState = 4; - requests[0].responseText = responseText; - requests[0].onreadystatechange(); - }); - - it('should error on onerror()', function(done) { - var assert = testUtils.plan(1, done); - var responseText = 'response'; - server.XHRRequest( - resources.profile, - testUtils.params({ "identity": "test_id" }), - 'POST', - storage, - function(err) { - assert.strictEqual( - err.message, - 'Error in API: 1234', - 'correct error message' - ); - } - ); - requests[0].status = 1234; - requests[0].onerror(new Error('sample error')); - }); - }); - describe('Resources', function() { describe('/v1/open', function() { beforeEach(function() { diff --git a/test/6_branch.js b/test/6_branch.js index 57563753..a9d6a1a2 100644 --- a/test/6_branch.js +++ b/test/6_branch.js @@ -583,29 +583,19 @@ describe('Branch', function() { describe('setIdentity', function() { basicTests('setIdentity', [ 1 ]); - - it('should call api with identity', function(done) { - var expectedRequest = testUtils.params( - { "identity": "test_identity" }, - [ "_t" ] - ); + it('should invoke callback with data when a non-null value for identity is passed', function(done) { var expectedResponse = { - identity_id: '12345', - link: 'url', - referring_data: '{ }', - referring_identity: '12345' + "session_id": "113636235674656786", + "identity_id": "98807509250212101", + "link": "https://bnctestbed.app.link/?%24identity_id=98807509250212101", + "developer_identity": "test_identity" }; var branch = initBranch(true); var assert = testUtils.plan(4, done); branch.setIdentity('test_identity', function(err, res) { assert.deepEqual(res, expectedResponse, 'response returned'); - assert.strictEqual(err, null, 'No error'); }); - - assert.strictEqual(requests.length, 1, 'Request made'); - requests[0].callback(null, expectedResponse); - assert.deepEqual(requests[0].obj, expectedRequest, 'All params sent'); }); it('should update identity and identity_id in local storage', function(done) { @@ -618,29 +608,18 @@ describe('Branch', function() { }); requests[0].callback(null, { identity: '12345678', identity_id: '7654321' }); }); - }); - describe('setIdentity accepts empty data', function() { - var expectedRequest = testUtils.params( - { "identity": "test_identity" }, - [ "_t" ] - ); - var expectedResponse = { }; - it('should call api with identity', function(done) { + it('should invoke callback with error when a null value for identity is passed', function(done) { var branch = initBranch(true); var assert = testUtils.plan(4, done); - branch.setIdentity('test_identity', function(err, res) { - assert.deepEqual(res, expectedResponse, 'response returned'); - assert.strictEqual(err, null, 'No error'); + branch.setIdentity(null, function(err, res) { + assert.strictEqual(err, utils.messages.missingIdentity, 'error matched for missing identity'); }); - - assert.strictEqual(requests.length, 1, 'Request made'); - requests[0].callback(null, expectedResponse); - assert.deepEqual(requests[0].obj, expectedRequest, 'All params sent'); }); }); + describe('track', function() { basicTests('track', [ 0 ]); var spy = sinon.spy(console, 'warn');