-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathoAuth_authorization_spec.js
More file actions
130 lines (120 loc) · 4.85 KB
/
oAuth_authorization_spec.js
File metadata and controls
130 lines (120 loc) · 4.85 KB
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
const sinon = require('sinon');
const cloudinary = require("../../../../cloudinary");
const helper = require("../../../spechelper");
const describe = require('../../../testUtils/suite');
const testConstants = require('../../../testUtils/testConstants');
const { PUBLIC_IDS } = testConstants;
const { PUBLIC_ID } = PUBLIC_IDS;
describe("oauth_token", function () {
it("should send the oauth_token option to the server (admin_api)", function () {
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.resource(PUBLIC_ID, { oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(requestSpy,
sinon.match.has("headers",
sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4")
));
});
});
it("should send the oauth_token config to the server (admin_api)", function () {
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
cloudinary.config({
api_key: undefined,
api_secret: undefined,
oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4'
});
await cloudinary.v2.api.resource(PUBLIC_ID).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(requestSpy,
sinon.match.has("headers",
sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4")
));
});
});
it("should not fail when only providing api_key and secret (admin_api)", function () {
cloudinary.config({
api_key: "1234",
api_secret: "1234",
oauth_token: undefined
});
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.api.resource(PUBLIC_ID).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(requestSpy, sinon.match.has("auth", "1234:1234"));
});
});
it("should fail when missing all credentials (admin_api)", function () {
cloudinary.config({
api_key: undefined,
api_secret: undefined,
oauth_token: undefined
});
expect(() => {
cloudinary.v2.api.resource(PUBLIC_ID)
}).to.throwError(/Must supply api_key/);
});
it("oauth_token as option should take priority with secret and key (admin_api)", function () {
cloudinary.config({
api_key: '1234',
api_secret: '1234'
});
return cloudinary.v2.api.resource(PUBLIC_ID, { oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' })
.then(
() => expect().fail()
).catch(({ error }) => expect(error.message).to.contain("Invalid token"));
});
it("should send the oauth_token option to the server (upload_api)", function () {
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.uploader.upload(PUBLIC_ID, { oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4' }).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(requestSpy,
sinon.match.has("headers",
sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4")
));
});
});
it("should send the oauth_token config to the server (upload_api)", function () {
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
cloudinary.config({
api_key: undefined,
api_secret: undefined,
oauth_token: 'MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4'
});
await cloudinary.v2.uploader.upload(PUBLIC_ID).catch(helper.ignoreApiFailure);
sinon.assert.calledWith(requestSpy,
sinon.match.has("headers",
sinon.match.has("Authorization", "Bearer MTQ0NjJkZmQ5OTM2NDE1ZTZjNGZmZjI4")
));
});
});
it("should not fail when only providing api_key and secret (upload_api)", function () {
cloudinary.config({
api_key: "1234",
api_secret: "1234",
oauth_token: undefined
});
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.uploader.upload(PUBLIC_ID).catch(helper.ignoreApiFailure);
const call = requestSpy.getCall(0);
expect(call.args[0]).not.to.have.property('auth');
});
});
it("should fail when missing all credentials (upload_api)", function () {
cloudinary.config({
api_key: undefined,
api_secret: undefined,
oauth_token: undefined
});
expect(() => {
cloudinary.v2.uploader.upload(PUBLIC_ID)
}).to.throwError(/Must supply api_key/);
});
it("should not need credentials for unsigned upload", function () {
cloudinary.config({
api_key: undefined,
api_secret: undefined,
oauth_token: undefined
});
return helper.provideMockObjects(async (mockXHR, writeSpy, requestSpy) => {
await cloudinary.v2.uploader.unsigned_upload(PUBLIC_ID, 'preset').catch(helper.ignoreApiFailure);
const call = requestSpy.getCall(0);
expect(call.args[0]).not.to.have.property('auth');
});
});
});