-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d37099
commit bc8cabc
Showing
3 changed files
with
298 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
const crypto = require('crypto'); | ||
const { formatEmail, calculateConversionObject } = require('./utils'); | ||
const { | ||
formatEmail, | ||
calculateConversionObject, | ||
fetchUserIds, | ||
curateUserInfoObject, | ||
deduceConversionRules, | ||
generateHeader, | ||
constructPartialStatus, | ||
createResponseArray, | ||
} = require('./utils'); | ||
const { InstrumentationError, ConfigurationError } = require('@rudderstack/integrations-lib'); | ||
const { API_HEADER_METHOD, API_PROTOCOL_VERSION, API_VERSION } = require('./config'); | ||
|
||
describe('formatEmail', () => { | ||
// Returns a hashed email when a valid email is passed as argument. | ||
|
@@ -31,3 +42,227 @@ describe('calculateConversionObject', () => { | |
expect(conversionObject).toEqual({ currencyCode: 'USD', amount: '0' }); | ||
}); | ||
}); | ||
|
||
describe('fetchUserIds', () => { | ||
// Throws an InstrumentationError when no user id is found in the message and no exception is caught | ||
it('should throw an InstrumentationError when no user id is found in the message and no exception is caught', () => { | ||
const message = {}; | ||
const destConfig = { | ||
hashData: true, | ||
}; | ||
expect(() => { | ||
fetchUserIds(message, destConfig); | ||
}).toThrow(InstrumentationError); | ||
}); | ||
it('should create user Ids array of objects with all allowed values', () => { | ||
const message = { | ||
context: { | ||
traits: { | ||
email: '[email protected]', | ||
}, | ||
externalId: [ | ||
{ | ||
type: 'LINKEDIN_FIRST_PARTY_ADS_TRACKING_UUID', | ||
id: 'abcdefg', | ||
}, | ||
{ | ||
type: 'ACXIOM_ID', | ||
id: '123456', | ||
}, | ||
{ | ||
type: 'ORACLE_MOAT_ID', | ||
id: '789012', | ||
}, | ||
], | ||
}, | ||
}; | ||
const destConfig = { | ||
hashData: true, | ||
}; | ||
const userIdArray = fetchUserIds(message, destConfig); | ||
expect(userIdArray).toEqual([ | ||
{ | ||
idType: 'SHA256_EMAIL', | ||
idValue: '48ddb93f0b30c475423fe177832912c5bcdce3cc72872f8051627967ef278e08', | ||
}, | ||
{ | ||
idType: 'LINKEDIN_FIRST_PARTY_ADS_TRACKING_UUID', | ||
idValue: 'abcdefg', | ||
}, | ||
{ | ||
idType: 'ACXIOM_ID', | ||
idValue: '123456', | ||
}, | ||
{ | ||
idType: 'ORACLE_MOAT_ID', | ||
idValue: '789012', | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('curateUserInfoObject', () => { | ||
// Returns a non-null object when given a message with both first and last name | ||
it('should return a non-null object when given a message with both first and last name and other properties', () => { | ||
const message = { | ||
context: { | ||
traits: { | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
title: 'Mr.', | ||
companyName: 'RudderTest', | ||
countryCode: 'USA', | ||
}, | ||
}, | ||
}; | ||
const result = curateUserInfoObject(message); | ||
expect(result).toEqual({ | ||
firstName: 'John', | ||
lastName: 'Doe', | ||
title: 'Mr.', | ||
companyName: 'RudderTest', | ||
countryCode: 'USA', | ||
}); | ||
}); | ||
// Returns a null object when given a message with an empty first name | ||
it('should return a null object when given a message without both first and last name', () => { | ||
const message = { | ||
context: { | ||
traits: { | ||
title: 'Mr.', | ||
companyName: 'RudderTest', | ||
countryCode: 'USA', | ||
}, | ||
}, | ||
}; | ||
const result = curateUserInfoObject(message); | ||
expect(result).toEqual(null); | ||
}); | ||
}); | ||
|
||
describe('deduceConversionRules', () => { | ||
// When conversionMapping is empty, return ConfigurationError | ||
it('should return ConfigurationError when conversionMapping is empty', () => { | ||
const trackEventName = 'eventName'; | ||
const destConfig = { | ||
conversionMapping: [], | ||
}; | ||
expect(() => deduceConversionRules(trackEventName, destConfig)).toThrow(ConfigurationError); | ||
}); | ||
|
||
// When conversionMapping is not empty, return the conversion rule | ||
it('should return the conversion rule when conversionMapping is not empty', () => { | ||
const trackEventName = 'eventName'; | ||
const destConfig = { | ||
conversionMapping: [{ from: 'eventName', to: 'conversionEvent' }], | ||
}; | ||
const result = deduceConversionRules(trackEventName, destConfig); | ||
expect(result).toEqual(['conversionEvent']); | ||
}); | ||
|
||
it('should return the conversion rule when conversionMapping is not empty', () => { | ||
const trackEventName = 'eventName'; | ||
const destConfig = { | ||
conversionMapping: [ | ||
{ from: 'eventName', to: 'conversionEvent' }, | ||
{ from: 'eventName', to: 'conversionEvent2' }, | ||
], | ||
}; | ||
const result = deduceConversionRules(trackEventName, destConfig); | ||
expect(result).toEqual(['conversionEvent', 'conversionEvent2']); | ||
}); | ||
}); | ||
|
||
describe('generateHeader', () => { | ||
// Returns a headers object with Content-Type, X-RestLi-Method, X-Restli-Protocol-Version, LinkedIn-Version, and Authorization keys when passed a valid access token. | ||
it('should return a headers object with all keys when passed a valid access token', () => { | ||
// Arrange | ||
const accessToken = 'validAccessToken'; | ||
|
||
// Act | ||
const result = generateHeader(accessToken); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
'Content-Type': 'application/json', | ||
'X-RestLi-Method': API_HEADER_METHOD, | ||
'X-Restli-Protocol-Version': API_PROTOCOL_VERSION, | ||
'LinkedIn-Version': API_VERSION, | ||
Authorization: `Bearer ${accessToken}`, | ||
}); | ||
}); | ||
|
||
// Returns a headers object with default values for all keys when passed an invalid access token. | ||
it('should return a headers object with default values for all keys when passed an invalid access token', () => { | ||
// Arrange | ||
const accessToken = 'invalidAccessToken'; | ||
|
||
// Act | ||
const result = generateHeader(accessToken); | ||
|
||
// Assert | ||
expect(result).toEqual({ | ||
'Content-Type': 'application/json', | ||
'X-RestLi-Method': API_HEADER_METHOD, | ||
'X-Restli-Protocol-Version': API_PROTOCOL_VERSION, | ||
'LinkedIn-Version': API_VERSION, | ||
Authorization: `Bearer ${accessToken}`, | ||
}); | ||
}); | ||
}); | ||
|
||
describe('constructPartialStatus', () => { | ||
// The function correctly constructs a map of error messages when given a string containing error messages. | ||
it('should correctly construct a map of error messages when given a string containing error messages', () => { | ||
const errorMessage = 'Index: 1, ERROR :: Error 1\nIndex: 2, ERROR :: Error 2\n'; | ||
const expectedErrorMap = { | ||
1: 'Error 1', | ||
2: 'Error 2', | ||
}; | ||
|
||
const result = constructPartialStatus(errorMessage); | ||
|
||
expect(result).toEqual(expectedErrorMap); | ||
}); | ||
|
||
// The function throws an error when given a non-string input. | ||
it('should throw an error when given a non-string input', () => { | ||
const errorMessage = 123; | ||
const result = constructPartialStatus(errorMessage); | ||
expect(result).toEqual({}); | ||
}); | ||
}); | ||
|
||
describe('createResponseArray', () => { | ||
// Returns an array of objects with statusCode, metadata and error properties | ||
it('should return an array of objects with statusCode, metadata and error properties', () => { | ||
// Arrange | ||
const metadata = [{ jobId: 1 }, { jobId: 2 }, { jobId: 3 }]; | ||
const partialStatus = { | ||
0: 'Partial status message 1', | ||
2: 'Partial status message 3', | ||
}; | ||
|
||
// Act | ||
const result = createResponseArray(metadata, partialStatus); | ||
|
||
// Assert | ||
expect(result).toEqual([ | ||
{ | ||
statusCode: 400, | ||
metadata: { jobId: 1 }, | ||
error: 'Partial status message 1', | ||
}, | ||
{ | ||
statusCode: 500, | ||
metadata: { jobId: 2 }, | ||
error: 'success', | ||
}, | ||
{ | ||
statusCode: 400, | ||
metadata: { jobId: 3 }, | ||
error: 'Partial status message 3', | ||
}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters