You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by gl...@apache.org on 2020/02/12 11:25:59 UTC
[couchdb-nano] 11/15: auth/session/uuids
This is an automated email from the ASF dual-hosted git repository.
glynnbird pushed a commit to branch jest
in repository https://gitbox.apache.org/repos/asf/couchdb-nano.git
commit 21597ff991ee6597ffd02ca18386329a66cdc4d9
Author: Glynn Bird <gl...@gmail.com>
AuthorDate: Fri Feb 7 17:09:20 2020 +0000
auth/session/uuids
---
lib/nano.js | 2 +-
test/nano.auth.test.js | 31 ++++++++++++++++++++++++++++
test/nano.session.test.js | 29 ++++++++++++++++++++++++++
test/nano.uuids.test.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 113 insertions(+), 1 deletion(-)
diff --git a/lib/nano.js b/lib/nano.js
index eb07b2d..0ca315f 100644
--- a/lib/nano.js
+++ b/lib/nano.js
@@ -469,8 +469,8 @@ module.exports = exports = function dbScope (cfg) {
function uuids (count, callback) {
if (typeof count === 'function') {
callback = count
- count = 1
}
+ count = count || 1
return relax({ method: 'GET', path: '_uuids', qs: { count: count } }, callback)
}
diff --git a/test/nano.auth.test.js b/test/nano.auth.test.js
new file mode 100644
index 0000000..f952887
--- /dev/null
+++ b/test/nano.auth.test.js
@@ -0,0 +1,31 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+const nock = require('nock')
+
+test('should be able to authenticate - POST /_session - nano.auth', async () => {
+ // mocks
+ const username = 'u'
+ const password = 'p'
+ const response = { ok: true, name: 'admin', roles: ['_admin', 'admin'] }
+ const scope = nock(COUCH_URL)
+ .post('/_session', 'name=u&password=p', { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8' })
+ .reply(200, response)
+
+ // test GET /_uuids
+ const p = await nano.auth(username, password)
+ expect(p).toStrictEqual(response)
+ expect(scope.isDone()).toBe(true)
+})
diff --git a/test/nano.session.test.js b/test/nano.session.test.js
new file mode 100644
index 0000000..ca2eb97
--- /dev/null
+++ b/test/nano.session.test.js
@@ -0,0 +1,29 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+const nock = require('nock')
+
+test('should be able to check your session - GET /_session - nano.auth', async () => {
+ // mocks
+ const response = { ok: true, userCtx: { name: null, roles: [] }, info: { authentication_db: '_users', authentication_handlers: ['cookie', 'default'] } }
+ const scope = nock(COUCH_URL)
+ .get('/_session')
+ .reply(200, response)
+
+ // test GET /_uuids
+ const p = await nano.session()
+ expect(p).toStrictEqual(response)
+ expect(scope.isDone()).toBe(true)
+})
diff --git a/test/nano.uuids.test.js b/test/nano.uuids.test.js
new file mode 100644
index 0000000..ea004f1
--- /dev/null
+++ b/test/nano.uuids.test.js
@@ -0,0 +1,52 @@
+// Licensed under the Apache License, Version 2.0 (the 'License'); you may not
+// use this file except in compliance with the License. You may obtain a copy of
+// the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT
+// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+// License for the specific language governing permissions and limitations under
+// the License.
+
+const Nano = require('..')
+const COUCH_URL = 'http://localhost:5984'
+const nano = Nano(COUCH_URL)
+const nock = require('nock')
+
+test('should be able to fetch uuids - GET /_uuids - nano.uuids', async () => {
+ // mocks
+ const response = {
+ uuids: [
+ 'c42ddf1272c7d05b2dc45b696200145f'
+ ]
+ }
+ const scope = nock(COUCH_URL)
+ .get('/_uuids?count=1')
+ .reply(200, response)
+
+ // test GET /_uuids
+ const p = await nano.uuids()
+ expect(p).toStrictEqual(response)
+ expect(scope.isDone()).toBe(true)
+})
+
+test('should be able to fetch more uuids - GET /_uuids?count=3 - nano.uuids', async () => {
+ // mocks
+ const response = {
+ uuids: [
+ 'c42ddf1272c7d05b2dc45b69620023df',
+ 'c42ddf1272c7d05b2dc45b6962002616',
+ 'c42ddf1272c7d05b2dc45b69620028cf'
+ ]
+ }
+ const scope = nock(COUCH_URL)
+ .get('/_uuids?count=3')
+ .reply(200, response)
+
+ // test GET /_uuids
+ const p = await nano.uuids(3)
+ expect(p).toStrictEqual(response)
+ expect(scope.isDone()).toBe(true)
+})