Skip to content

Commit

Permalink
Expanded analytics redirects tests to cover both getstream.io and str…
Browse files Browse the repository at this point in the history
…eam-io-api.com
  • Loading branch information
dwightgunning committed Oct 12, 2017
1 parent 96dad68 commit e555bf2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 9 deletions.
16 changes: 10 additions & 6 deletions src/lib/client.js
Expand Up @@ -28,7 +28,7 @@ var StreamClient = function() {

StreamClient.prototype = {
baseUrl: 'https://api.stream-io-api.com/api/',
baseAnalyticsUrl: 'https://analytics.getstream.io/analytics/',
baseAnalyticsUrl: 'https://analytics.stream-io-api.com/analytics/',

initialize: function(apiKey, apiSecret, appId, options) {
/**
Expand Down Expand Up @@ -60,9 +60,9 @@ StreamClient.prototype = {
this.expireTokens = this.options.expireTokens ? this.options.expireTokens : false;
// which data center to use
this.location = this.options.location;

var protocol = this.options.protocol || 'https';

if (this.location) {
this.baseUrl = protocol + '://' + this.location + '-api.stream-io-api.com/api/';
}
Expand All @@ -79,6 +79,10 @@ StreamClient.prototype = {
this.baseUrl = process.env.STREAM_BASE_URL;
}

if (typeof (process) !== 'undefined' && process.env.STREAM_ANALYTICS_BASE_URL) {
this.baseAnalyticsUrl = process.env.STREAM_ANALYTICS_BASE_URL;
}

this.handlers = {};
this.browser = typeof (window) !== 'undefined';
this.node = !this.browser;
Expand Down Expand Up @@ -492,13 +496,13 @@ StreamClient.prototype = {
signature: authToken,
}, callback);
},

updateActivity: function(activity) {
/**
* Updates one activity on the getstream-io api
* @since 3.1.0
* @param {object} activity The activity to update
* @return {Promise}
* @return {Promise}
*/
return this.updateActivities([activity]);
},
Expand All @@ -517,7 +521,7 @@ if (qs) {
* @param {string} userId User id to track
* @param {array} events List of events to track
* @return {string} The redirect url
*/
*/
var uri = url.parse(targetUrl);

if (!(uri.host || (uri.hostname && uri.port)) && !uri.isUnix) {
Expand Down
53 changes: 50 additions & 3 deletions test/unit/node/redirect_test.js
@@ -1,17 +1,61 @@
var config = require('../utils/config')
, StreamClient = require('../utils/mocks').StreamClient
, jwt = require('jsonwebtoken')
, url = require('url')
, request = require('request')
, expect = require('expect.js')
, afterEachFn = require('../utils/hooks').afterEach
, beforeEachFn = require('../utils/hooks').beforeEach
, errors = require('../../../src/lib/errors')
, qs = require('qs');

describe('[UNIT] Redirect URL\'s', function() {

beforeEach(beforeEachFn);
// beforeEach(beforeEachFn);

it('should create email redirects', function() {
it('should create email redirects (analytics.stream-io-api.com)', function() {
var expectedParts = ['https://analytics.stream-io-api.com/analytics/redirect/',
'auth_type=jwt',
'url=http%3A%2F%2Fgoogle.com%2F%3Fa%3Db%26c%3Dd',
'events=%5B%7B%22foreign_ids%22%3A%5B%22tweet%3A1%22%2C%22tweet%3A2%22%2C%22tweet%3A3%22%2C%22tweet%3A4%22%2C%22tweet%3A5%22%5D%2C%22user_id%22%3A%22tommaso%22%2C%22location%22%3A%22email%22%2C%22feed_id%22%3A%22user%3Aglobal%22%7D%2C%7B%22foreign_id%22%3A%22tweet%3A1%22%2C%22label%22%3A%22click%22%2C%22position%22%3A3%2C%22user_id%22%3A%22tommaso%22%2C%22location%22%3A%22email%22%2C%22feed_id%22%3A%22user%3Aglobal%22%7D%5D',
'api_key=' + config.API_KEY,
];
var engagement = {
'foreign_id': 'tweet:1',
'label': 'click',
'position': 3,
'user_id': 'tommaso',
'location': 'email',
'feed_id': 'user:global'
},
impression = {
'foreign_ids': ['tweet:1', 'tweet:2', 'tweet:3', 'tweet:4', 'tweet:5'],
'user_id': 'tommaso',
'location': 'email',
'feed_id': 'user:global'
},
events = [impression, engagement],
userId = 'tommaso',
targetUrl = 'http://google.com/?a=b&c=d';

this.client = new StreamClient(config.API_KEY, config.API_SECRET);
var redirectUrl = this.client.createRedirectUrl(targetUrl, userId, events);

var queryString = qs.parse(url.parse(redirectUrl).query);
var decoded = jwt.verify(queryString.authorization, config.API_SECRET);

expect(decoded).to.eql({
'resource': 'redirect_and_track',
'action': '*',
'user_id': userId,
});

for (var i = 0; i < expectedParts.length; i++) {
expect(redirectUrl).to.contain(expectedParts[i]);
}
});

it('should create email redirects (analytics.getstream.io)', function() {
var expectedParts = ['https://analytics.getstream.io/analytics/redirect/',
'auth_type=jwt',
'url=http%3A%2F%2Fgoogle.com%2F%3Fa%3Db%26c%3Dd',
Expand All @@ -35,6 +79,8 @@ describe('[UNIT] Redirect URL\'s', function() {
events = [impression, engagement],
userId = 'tommaso',
targetUrl = 'http://google.com/?a=b&c=d';
process.env.STREAM_ANALYTICS_BASE_URL = 'https://analytics.getstream.io/analytics/'
this.client = new StreamClient(config.API_KEY, config.API_SECRET);
var redirectUrl = this.client.createRedirectUrl(targetUrl, userId, events);

var queryString = qs.parse(url.parse(redirectUrl).query);
Expand All @@ -49,6 +95,7 @@ describe('[UNIT] Redirect URL\'s', function() {
for (var i = 0; i < expectedParts.length; i++) {
expect(redirectUrl).to.contain(expectedParts[i]);
}
delete process.env['STREAM_ANALYTICS_BASE_URL']
});

it('should follow redirect urls', function(done) {
Expand Down Expand Up @@ -78,4 +125,4 @@ describe('[UNIT] Redirect URL\'s', function() {
}).to.throwException(errors.MissingSchemaError);
});

});
});

0 comments on commit e555bf2

Please sign in to comment.