This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 63
/
test.js
212 lines (177 loc) · 5.99 KB
/
test.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
var WooCommerce = require('./index.js');
var chai = require('chai');
var nock = require('nock');
var Agent = require('http').Agent;
var HttpsAgent = require('https').Agent;
var agent = new Agent();
var httpsAgent = new HttpsAgent();
describe('#Construct', function() {
it('should throw an error if the url, consumerKey or consumerSecret are missing', function() {
chai.expect(function() {
new WooCommerce();
}).to.throw(Error);
});
it('should set the default options', function() {
var api = new WooCommerce({
url: 'http://test.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
version: 'wc/v1',
agent: agent
});
chai.expect(api.version).to.equal('wc/v1');
chai.expect(api.isSsl).to.be.false;
chai.expect(api.verifySsl).to.be.true;
chai.expect(api.encoding).to.equal('utf8');
chai.expect(api.agent).to.equal(agent);
});
});
describe('#Requests', function() {
beforeEach(function() {
nock.cleanAll();
});
var api = new WooCommerce({
url: 'https://test.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
version: 'wc/v1',
agent: httpsAgent
});
it('should return full API url', function() {
var endpoint = 'products';
var expected = 'https://test.dev/wp-json/wc/v1/products';
var url = api._getUrl(endpoint);
chai.assert.equal(url, expected);
});
it('should return full WP REST API url with a custom path', function() {
var restApi = new WooCommerce({
url: 'https://test.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
wpAPIPrefix: 'wp-rest',
version: 'wc/v1',
agent: httpsAgent
});
var endpoint = 'products';
var expected = 'https://test.dev/wp-rest/wc/v1/products';
var url = restApi._getUrl(endpoint);
chai.assert.equal(url, expected);
});
it('should return sorted by name query string', function() {
var url = 'http://test.dev/wp-json/wc/v1/products?filter[q]=Woo+Album&fields=id&filter[limit]=1';
var expected = 'http://test.dev/wp-json/wc/v1/products?fields=id&filter[limit]=1&filter[q]=Woo%20Album';
var normalized = api._normalizeQueryString(url);
chai.assert.equal(normalized, expected);
});
it('should return content for basic auth', function(done) {
nock('https://test.dev/wp-json/wc/v1').post('/orders', {}).reply(200, {
ok: true
});
api.post('orders', {}, function(err, data) {
chai.expect(err).to.not.exist;
chai.expect(data).be.a.string;
done();
});
});
it('should return content for get requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').get('/orders').reply(200, {
ok: true
});
api.get('orders', function(err, data) {
chai.expect(err).to.not.exist;
chai.expect(data).be.a.string;
done();
});
});
it('should return content for put requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').put('/orders', {}).reply(200, {
ok: true
});
api.put('orders', {}, function(err, data) {
chai.expect(err).to.not.exist;
chai.expect(data).be.a.string;
done();
});
});
it('should return content for delete requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').delete('/orders').reply(200, {
ok: true
});
api.delete('orders', function(err, data) {
chai.expect(err).to.not.exist;
chai.expect(data).be.a.string;
done();
});
});
it('should return content for options requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').intercept('/orders', 'OPTIONS').reply(400);
api.options('orders', function(err, data) {
chai.expect(err).to.not.exist;
chai.expect(data).be.a.string;
done();
});
});
it('should return a promise for getAsync requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').get('/orders').reply(200, {
ok: true
});
api.getAsync('orders').finally(function(data) {
chai.expect(data).be.a.string;
done();
});
});
it('should return a promise for postAsync requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').post('/orders', {}).reply(200, {
ok: true
});
api.postAsync('orders', {}).finally(function(data) {
chai.expect(data).be.a.string;
done();
});
});
it('should return a promise for putAsync requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').put('/orders', {}).reply(200, {
ok: true
});
api.putAsync('orders', {}).finally(function(data) {
chai.expect(data).be.a.string;
done();
});
});
it('should return a promise for deleteAsync requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').delete('/orders').reply(200, {
ok: true
});
api.deleteAsync('orders').finally(function(data) {
chai.expect(data).be.a.string;
done();
});
});
it('should return a promise for optionsAsync requests', function(done) {
nock('https://test.dev/wp-json/wc/v1').intercept('/orders', 'OPTIONS').reply(400);
api.optionsAsync('orders').finally(function(data) {
chai.expect(data).be.a.string;
done();
});
});
it('should return content for OAuth', function(done) {
var oAuth = new WooCommerce({
url: 'http://test.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
wpAPI: true,
version: 'wc/v1'
});
nock('http://test.dev/wp-json/wc/v1').filteringPath(/\?.*/, '?params').get('/orders?params').reply(200, {
ok: true
});
oAuth.get('orders', function(err, data) {
chai.expect(err).to.not.exist;
chai.expect(data).be.a.string;
done();
});
});
});