|
1 | | -// Copyright © 2017 IBM Corp. All rights reserved. |
| 1 | +// Copyright © 2017, 2019 IBM Corp. All rights reserved. |
2 | 2 | // |
3 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | 4 | // you may not use this file except in compliance with the License. |
@@ -31,6 +31,8 @@ const TOKEN_SERVER = 'https://iam.bluemix.net'; |
31 | 31 | const DBNAME = `/nodejs-cloudant-${uuidv4()}`; |
32 | 32 |
|
33 | 33 | // mocks |
| 34 | +const MOCK_TOKEN_SERVER_USER = 'ASPialYTheOS'; |
| 35 | +const MOCK_TOKEN_SERVER_PASS = 'Zr28yT54^y!Kk&$M'; |
34 | 36 | const MOCK_ACCESS_TOKEN = 'eyJraWQiOiIyMDE3MDQwMi0wMDowMDowMCIsImFsZyI6IlJTMj' + |
35 | 37 | 'U2In0.eeyJraWQiOiIyMDE3MDQwMi0wMDowMDowMCIsImFsZyI6IlJTMjU2In0.eyJpYW1faWQiO' + |
36 | 38 | 'iJJQk1pZC0yNzAwMDdHRjBEIiwiaWQiOiJJQk1pZC0yNzAwMDdHRjBEIiwicmVhbG1pZCI6IklCT' + |
@@ -143,6 +145,45 @@ describe('#db IAMAuth Plugin', function() { |
143 | 145 | }); |
144 | 146 | }); |
145 | 147 |
|
| 148 | + it('performs request and returns 200 response when authenticating with IAM token service', function(done) { |
| 149 | + if (process.env.NOCK_OFF) { |
| 150 | + this.skip(); |
| 151 | + } |
| 152 | + |
| 153 | + var iamMocks = nock(TOKEN_SERVER) |
| 154 | + .post('/identity/token', { |
| 155 | + 'grant_type': 'urn:ibm:params:oauth:grant-type:apikey', |
| 156 | + 'response_type': 'cloud_iam', |
| 157 | + 'apikey': IAM_API_KEY |
| 158 | + }) |
| 159 | + .basicAuth({ user: MOCK_TOKEN_SERVER_USER, pass: MOCK_TOKEN_SERVER_PASS }) |
| 160 | + .reply(200, MOCK_IAM_TOKEN_RESPONSE); |
| 161 | + |
| 162 | + var cloudantMocks = nock(SERVER) |
| 163 | + .post('/_iam_session', {access_token: MOCK_ACCESS_TOKEN}) |
| 164 | + .reply(200, {ok: true}, MOCK_SET_IAM_SESSION_HEADER) |
| 165 | + .get(DBNAME) |
| 166 | + .reply(200, {doc_count: 0}); |
| 167 | + |
| 168 | + var cloudantClient = new Client({ plugins: { iamauth: { |
| 169 | + iamApiKey: IAM_API_KEY, |
| 170 | + iamClientId: MOCK_TOKEN_SERVER_USER, |
| 171 | + iamClientSecret: MOCK_TOKEN_SERVER_PASS |
| 172 | + }}}); |
| 173 | + var req = { url: SERVER + DBNAME, method: 'GET' }; |
| 174 | + cloudantClient.request(req, function(err, resp, data) { |
| 175 | + assert.equal(err, null); |
| 176 | + if (!process.env.NOCK_OFF) { |
| 177 | + assert.equal(resp.request.headers.cookie, MOCK_IAM_SESSION); |
| 178 | + } |
| 179 | + assert.equal(resp.statusCode, 200); |
| 180 | + assert.ok(data.indexOf('"doc_count":0') > -1); |
| 181 | + iamMocks.done(); |
| 182 | + cloudantMocks.done(); |
| 183 | + done(); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
146 | 187 | it('performs multiple requests that return 200 responses with only a single session request', function(done) { |
147 | 188 | // NOTE: Use NOCK_OFF=true to test using a real CouchDB instance. |
148 | 189 | var iamMocks = nock(TOKEN_SERVER) |
|
0 commit comments