forked from woocommerce/wc-api-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
118 lines (98 loc) · 3.35 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
var WooCommerce = require('./index.js');
var chai = require('chai');
var nock = require('nock');
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'
});
chai.expect(api.version).to.equal('v3');
chai.expect(api.isSsl).to.be.false;
chai.expect(api.verifySsl).to.be.true;
chai.expect(api.encoding).to.equal('utf8');
});
});
describe('#Requests', function() {
beforeEach(function() {
nock.cleanAll();
});
var api = new WooCommerce({
url: 'https://test.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
});
it('should return full API url', function() {
var endpoint = 'products';
var expected = 'https://test.dev/wc-api/v3/products';
var url = api._getUrl(endpoint);
chai.assert.equal(url, expected);
});
it('should return sorted by name query string', function() {
var url = 'http://test.dev/wc-api/v3/products?filter[q]=Woo+Album&fields=id&filter[limit]=1';
var expected = 'http://test.dev/wc-api/v3/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/wc-api/v3').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/wc-api/v3').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/wc-api/v3').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/wc-api/v3').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 OAuth', function(done) {
var oAuth = new WooCommerce({
url: 'http://test.dev',
consumerKey: 'ck_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
consumerSecret: 'cs_XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
});
nock('http://test.dev/wc-api/v3').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();
});
});
});