leadconduit-batch 0.1.8 → 0.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/delivery.js CHANGED
@@ -1,14 +1,10 @@
1
1
  const flatten = require('flat').flatten;
2
2
  const _ = require('lodash');
3
-
3
+ const baseUrl = require('@activeprospect/leadconduit-base-url');
4
4
 
5
5
  const encodeAuthentication = apiKey =>
6
6
  `Basic ${new Buffer(`API:${apiKey}`).toString('base64')}`;
7
7
 
8
- //
9
- // Request Function -------------------------------------------------------
10
- //
11
-
12
8
  const request = function(vars) {
13
9
 
14
10
  let key, value;
@@ -37,7 +33,7 @@ const request = function(vars) {
37
33
 
38
34
  return {
39
35
  method: 'POST',
40
- url: `${getBaseUrl()}${vars.batch_id}/records`,
36
+ url: `${baseUrl('batch')}/batches/${vars.batch_id}/records`,
41
37
  headers: {
42
38
  Accept: 'application/json',
43
39
  Authorization: encodeAuthentication(vars.activeprospect.api_key),
@@ -53,23 +49,12 @@ request.variables = function() {
53
49
  { name: 'batch.custom.*', type: 'wildcard', required: false }
54
50
  ];
55
51
  };
56
-
57
- //
58
- // Validate Function ------------------------------------------------------
59
- //
60
-
61
52
  const validate = function(vars) {
62
53
  if (_.isNil(vars.batch_id) || !vars.batch_id.match(/^[0-9a-fA-F]{24}$/)) {
63
54
  return 'must have a valid batch id';
64
55
  }
65
56
  };
66
57
 
67
-
68
- //
69
- // Response Function ------------------------------------------------------
70
- //
71
-
72
-
73
58
  const response = function(vars, req, res) {
74
59
  let event = {};
75
60
  switch (res.status) {
@@ -103,28 +88,6 @@ response.variables = function() {
103
88
  ];
104
89
  };
105
90
 
106
- //
107
- // Helpers ----------------------------------------------------------------
108
- //
109
-
110
- const getBaseUrl = function() {
111
- switch (process.env.NODE_ENV) {
112
- case 'production':
113
- case 'test':
114
- return 'https://batch.leadconduit.com/batches/';
115
- case 'staging':
116
- return 'https://batch.leadconduit-staging.com/batches/';
117
- case 'development':
118
- return 'https://batch.leadconduit-development.com/batches/';
119
- default:
120
- return 'http://batch.leadconduit.localhost/batches/';
121
- }
122
- };
123
-
124
- //
125
- // Exports ----------------------------------------------------------------
126
- //
127
-
128
91
  module.exports = {
129
92
  validate,
130
93
  request,
package/package.json CHANGED
@@ -1,30 +1,25 @@
1
1
  {
2
2
  "name": "leadconduit-batch",
3
3
  "description": "LeadConduit integration to send batch files",
4
- "contributors": [
5
- "Kelly Martin <kelly@activeprospect.com>",
6
- "Jonathan Grayson <jonathan@activeprospect.com>"
7
- ],
8
- "version": "0.1.8",
4
+ "version": "0.1.10",
9
5
  "main": "index.js",
10
- "license": "CC-BY-NC-ND-4.0",
6
+ "license": "UNLICENSED",
11
7
  "repository": {
12
8
  "type": "git",
13
9
  "url": "https://github.com/activeprospect/leadconduit-integration-batch"
14
10
  },
15
- "engines": {
16
- "node": ">=0.8.1"
17
- },
18
11
  "scripts": {
19
- "test": "NODE_ENV=test TZ=GMT ./node_modules/.bin/mocha --recursive --reporter spec --colors --timeout 10000 spec/"
12
+ "test": "mocha",
13
+ "fixlint": "eslint . --fix",
14
+ "lint": "eslint ."
20
15
  },
21
16
  "dependencies": {
22
- "flat": "1.2.1",
17
+ "@activeprospect/leadconduit-base-url": "^1.0.0",
18
+ "flat": "^5.0.2",
23
19
  "lodash": "^4.17.4"
24
20
  },
25
21
  "devDependencies": {
26
- "chai": ">= 1.9.0",
27
- "leadconduit-integration": "^0.2.0",
28
- "mocha": "^8.3.0"
22
+ "@activeprospect/integration-dev-dependencies": "^2.0.2",
23
+ "leadconduit-integration": "^0.6.3"
29
24
  }
30
25
  }
@@ -0,0 +1,196 @@
1
+ const assert = require('chai').assert;
2
+ const integration = require('../lib/delivery');
3
+ const parser = require('leadconduit-integration').test.types.parser(integration.request.variables());
4
+
5
+ const withAPIKey = function(vars) {
6
+ vars.activeprospect = {
7
+ api_key: 'c9351ff49a8e38a23493c6b7328c7629'
8
+ };
9
+ return vars;
10
+ };
11
+
12
+ describe('Batch Request', function() {
13
+ beforeEach(function() {
14
+ this.vars = parser(
15
+ withAPIKey({
16
+ lead: {
17
+ email: 'foo@bar.com',
18
+ first_name: 'Joe',
19
+ state: null
20
+ },
21
+ batch_id: '57ed7d52d0c92f2077eaac1f',
22
+ foo: null,
23
+ batch: {
24
+ custom: {
25
+ email: 'bar@foo.com',
26
+ favorite_color: 'blue',
27
+ least_favorite_color: null
28
+ }
29
+ }
30
+ })
31
+ );
32
+ this.request = integration.request(this.vars);
33
+ });
34
+
35
+ afterEach(function() {
36
+ process.env.NODE_ENV = 'test';
37
+ });
38
+
39
+ it('should have url', function() {
40
+ assert.equal(this.request.url, 'https://batch.leadconduit.com/batches/57ed7d52d0c92f2077eaac1f/records');
41
+ });
42
+
43
+ it('uses the api_key in the auth header', function() {
44
+ assert.equal(this.request.headers.Authorization, 'Basic QVBJOmM5MzUxZmY0OWE4ZTM4YTIzNDkzYzZiNzMyOGM3NjI5');
45
+ });
46
+
47
+ it('should be POST', function() {
48
+ assert.equal(this.request.method, 'POST');
49
+ });
50
+
51
+ it('should set the content type to JSON', function() {
52
+ assert.equal(this.request.headers['Content-Type'], 'application/json');
53
+ });
54
+
55
+ it('should send the correct value of fields in lead', function() {
56
+ assert.equal(JSON.parse(this.request.body).first_name, 'Joe');
57
+ });
58
+
59
+ it('should send the correct value of custom fields', function() {
60
+ assert.equal(JSON.parse(this.request.body).favorite_color, 'blue');
61
+ });
62
+
63
+ it('should override lead values with batch.custom values of the same name', function() {
64
+ assert.equal(JSON.parse(this.request.body).email, 'bar@foo.com');
65
+ });
66
+
67
+ it('should not send null fields', function() {
68
+ assert.isUndefined(JSON.parse(this.request.body).state);
69
+ assert.isUndefined(JSON.parse(this.request.body).foo);
70
+ assert.isUndefined(JSON.parse(this.request.body).least_favorite_color);
71
+ });
72
+
73
+ it('should handle no custom fields', function() {
74
+ assert.isUndefined(JSON.parse(integration.request(withAPIKey({lead: {email: 'foo@bar.com'}, batch_id: '57ed7d52d0c92f2077eaac1f'})).body).favorite_color);
75
+ });
76
+
77
+ it('should delete batch_id from content', function() {
78
+ assert.isUndefined(JSON.parse(this.request.body).batch_id, 'batch_id is in body');
79
+ });
80
+
81
+ it('should set production url correctly', function() {
82
+ process.env.NODE_ENV = 'production';
83
+ assert.equal(integration.request(this.vars).url, 'https://batch.leadconduit.com/batches/57ed7d52d0c92f2077eaac1f/records');
84
+ });
85
+
86
+ it('should set staging url correctly', function() {
87
+ process.env.NODE_ENV = 'staging';
88
+ assert.equal(integration.request(this.vars).url, 'https://batch.leadconduit-staging.com/batches/57ed7d52d0c92f2077eaac1f/records');
89
+ });
90
+
91
+ it('should set development url correctly', function() {
92
+ process.env.NODE_ENV = 'development';
93
+ assert.equal(integration.request(this.vars).url, 'https://batch.leadconduit-development.com/batches/57ed7d52d0c92f2077eaac1f/records');
94
+ });
95
+
96
+ it('should set local url correctly', function() {
97
+ process.env.NODE_ENV = 'local';
98
+ assert.equal(integration.request(this.vars).url, 'http://batch.leadconduit.localhost/batches/57ed7d52d0c92f2077eaac1f/records');
99
+ });
100
+
101
+ describe('batch_id is set using custom wildcard', function() {
102
+
103
+ beforeEach(function() {
104
+ this.vars.batch.custom.batch_id = '457ed7d52d0c92f2077eaac1f';
105
+ this.request = integration.request(this.vars);
106
+ });
107
+
108
+ it('should set batch_id in content to the custom wildcard value', function() {
109
+ assert.equal(JSON.parse(this.request.body).batch_id, this.vars.batch.custom.batch_id);
110
+ });
111
+
112
+ it('should use base batch_id to set the url', function() {
113
+ assert.equal(this.request.url, 'https://batch.leadconduit.com/batches/57ed7d52d0c92f2077eaac1f/records');
114
+ });
115
+ });
116
+ });
117
+
118
+ describe('Validate Function', function() {
119
+
120
+ it('should not return valid when batch_id is missing', function() {
121
+ let vars = {
122
+ lead: { email: 'foo@bar.com' },
123
+ chair: 'Steelcase Leap'
124
+ };
125
+ assert.equal(integration.validate(vars), 'must have a valid batch id');
126
+ });
127
+
128
+ it('should not return valid when batch_id is an invalid format', function() {
129
+ let vars = { batch_id: '12345' };
130
+ assert.equal(integration.validate(vars), 'must have a valid batch id');
131
+ });
132
+
133
+ it('should not return valid when batch_id is an empty string', function() {
134
+ let vars = { batch_id: '' };
135
+ assert.equal(integration.validate(vars), 'must have a valid batch id');
136
+ });
137
+ });
138
+
139
+ describe('Response', function() {
140
+
141
+ describe('Success', function() {
142
+
143
+ it('should set a success outcome for a 200 status code', function() {
144
+ let res = { status: 200 };
145
+ let expected = { outcome: 'success' };
146
+ let response = integration.response({}, {}, res);
147
+ assert.deepEqual(expected, response);
148
+ });
149
+
150
+ });
151
+
152
+
153
+ describe('Error', function() {
154
+
155
+ it('should return invalid batch id error on 400 status code', function() {
156
+ let res = { status: 400 };
157
+ let expected = {
158
+ outcome: 'error',
159
+ reason: 'invalid batch id'
160
+ };
161
+ let response = integration.response({}, {}, res);
162
+ assert.deepEqual(expected, response);
163
+ });
164
+
165
+ it('should return unauthorized on 401 status code', function() {
166
+ let res = { status: 401 };
167
+ let expected = {
168
+ outcome: 'error',
169
+ reason: 'unauthorized'
170
+ };
171
+ let response = integration.response({}, {}, res);
172
+ assert.deepEqual(expected, response);
173
+ });
174
+
175
+ it('should return batch not found on 404 status code', function() {
176
+ let res = { status: 404 };
177
+ let expected = {
178
+ outcome: 'error',
179
+ reason: 'batch not found'
180
+ };
181
+ let response = integration.response({}, {}, res);
182
+ assert.deepEqual(expected, response);
183
+ });
184
+
185
+ it('should return unknown error on any status code other than 200 or 400', function() {
186
+ let res = { status: 500 };
187
+ let expected = {
188
+ outcome: 'error',
189
+ reason: 'unknown error: undefined'
190
+ };
191
+ let response = integration.response({}, {}, res);
192
+ assert.deepEqual(expected, response);
193
+ });
194
+ });
195
+
196
+ });