Skip to content

Commit

Permalink
feat(core): remove v1/profile call inside setIdentity (#916)
Browse files Browse the repository at this point in the history
- feat(core): remove v1/profile call inside setIdentity
  • Loading branch information
JagadeeshKaricherla-branch authored Aug 9, 2023
1 parent 8ab9694 commit 32886f5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 179 deletions.
3 changes: 2 additions & 1 deletion src/1_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
};

/**
Expand Down
10 changes: 0 additions & 10 deletions src/2_resources.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
40 changes: 15 additions & 25 deletions src/6_branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
});

/**
Expand Down
8 changes: 8 additions & 0 deletions test/1_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
113 changes: 0 additions & 113 deletions test/3_api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
39 changes: 9 additions & 30 deletions test/6_branch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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');
Expand Down

0 comments on commit 32886f5

Please sign in to comment.