-
Notifications
You must be signed in to change notification settings - Fork 223
/
event-request_tests.js
190 lines (182 loc) · 7.94 KB
/
event-request_tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';
const {
Event,
EventRequest,
EventResponse,
FacebookAdsApi,
HttpMethod,
HttpServiceClientConfig,
ServerEvent,
UserData,
} = require('facebook-nodejs-business-sdk');
const {describe} = require('mocha');
const { expect } = require('chai');
describe('EventRequest', function() {
describe('setHttpService', function() {
it('event_request setClient overrides the HttpServiceClientConfig client', async function() {
const expected_response = new EventResponse(
0,
['messages-1'],
'fb-trace-id-2',
'id-3',
4
);
let actual_url, actual_method, actual_headers, actual_params;
const mock_http_service = {
executeRequest: (url, method, headers, params) => {
return new Promise((resolve, reject) => {
actual_url = url;
actual_method = method;
actual_headers = headers;
actual_params = params;
return resolve(expected_response);
});
}
};
const mock_http_service_unused = {
executeRequest: (url, method, headers, params) => {
throw new Exception('should not have be called');
}
}
HttpServiceClientConfig.setClient(mock_http_service_unused);
const user_data = (new UserData()).setEmail('joe@eg.com');
const server_event = (new ServerEvent())
.setEventName('Purchase')
.setEventTime(Math.floor(new Date() / 1000))
.setUserData(user_data)
const access_token = 'access-token-0';
const pixel_id = 'pixel-id-1';
const event_request = (new EventRequest(access_token, pixel_id))
.setEvents([server_event])
.setTestEventCode('test-code-2')
.setHttpService(mock_http_service);
const expected_url =`${FacebookAdsApi.GRAPH}/${FacebookAdsApi.VERSION}/${pixel_id}/events`
const expected_method = HttpMethod.POST;
const expected_headers = {
'User-Agent': `fbbizsdk-nodejs-${FacebookAdsApi.VERSION}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
};
const expected_params = {
data: [server_event.normalize.call(server_event)],
partner_agent: event_request.partner_agent,
test_event_code: event_request.test_event_code,
namespace_id: event_request.namespace_id,
upload_id: event_request.upload_id,
upload_tag: event_request.upload_tag,
upload_source: event_request.upload_source,
test_event_code: event_request.test_event_code,
access_token: access_token,
};
let actual_response;
const result = await event_request.execute()
.then(response => {
actual_response = response;
})
.catch(exception => {
console.error(exception);
});
expect(actual_response).to.deep.equal(expected_response);
expect(actual_url).to.equal(expected_url);
expect(actual_method).to.equal(HttpMethod.POST);
expect(actual_headers).to.deep.equal(expected_headers);
expect(actual_params).to.deep.equal(expected_params);
});
it('HttpServiceClientConfig client gets used', async function() {
const expected_response = new EventResponse(
0,
['messages-1'],
'fb-trace-id-2',
'id-3',
4
);
let actual_url, actual_method, actual_headers, actual_params;
const mock_http_service = {
executeRequest: (url, method, headers, params) => {
return new Promise((resolve, reject) => {
actual_url = url;
actual_method = method;
actual_headers = headers;
actual_params = params;
return resolve(expected_response);
});
}
};
HttpServiceClientConfig.setClient(mock_http_service);
const pixel_id = 'pixel-id-1';
const user_data = (new UserData()).setEmail('joe@eg.com');
const server_event = (new ServerEvent())
.setEventName('Purchase')
.setEventTime(Math.floor(new Date() / 1000))
.setUserData(user_data)
const access_token = 'access-token-0';
const event_request = (new EventRequest(access_token, pixel_id))
.setEvents([server_event])
.setTestEventCode('test-code-2');
const expected_url =`${FacebookAdsApi.GRAPH}/${FacebookAdsApi.VERSION}/${pixel_id}/events`
const expected_method = HttpMethod.POST;
const expected_headers = {
'User-Agent': `fbbizsdk-nodejs-${FacebookAdsApi.VERSION}`,
'Content-Type': 'application/json',
'Accept': 'application/json',
};
const expected_params = {
data: [server_event.normalize.call(server_event)],
partner_agent: event_request.partner_agent,
test_event_code: event_request.test_event_code,
namespace_id: event_request.namespace_id,
upload_id: event_request.upload_id,
upload_tag: event_request.upload_tag,
upload_source: event_request.upload_source,
test_event_code: event_request.test_event_code,
access_token: access_token,
};
let actual_response;
const result = await event_request.execute()
.then(response => {
actual_response = response;
})
.catch(exception => {
console.error(exception);
});
expect(actual_response).to.deep.equal(expected_response);
expect(actual_url).to.equal(expected_url);
expect(actual_method).to.equal(HttpMethod.POST);
expect(actual_headers).to.deep.equal(expected_headers);
expect(actual_params).to.deep.equal(expected_params);
});
});
it('cloneWithoutEvents clones the EventRequest object without the events', async function() {
const event_request = new EventRequest(
'access_token-1',
'pixel_id-2',
[new Event()],
'partner_agent-3',
'test_event_code-4',
'namespace_id-5',
'upload_id-6',
'upload_tag-7',
'upload_source-8',
true
);
const cloned = event_request.cloneWithoutEvents();
expect(cloned.access_token).to.equal(event_request.access_token);
expect(cloned.pixel_id).to.equal(event_request.pixel_id);
expect(cloned.events).to.deep.equal([]);
expect(cloned.partner_agent).to.equal(event_request.partner_agent);
expect(cloned.test_event_code).to.equal(event_request.test_event_code);
expect(cloned.namespace_id).to.equal(event_request.namespace_id);
expect(cloned.upload_id).to.equal(event_request.upload_id);
expect(cloned.upload_tag).to.equal(event_request.upload_tag);
expect(cloned.upload_source).to.equal(event_request.upload_source);
expect(cloned.debug_mode).to.equal(event_request.debug_mode);
expect(cloned === event_request).to.equal(false);
});
});