Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(#9): adds support for CouchDb v2 #10

Merged
merged 8 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@ jobs:
- run: npm run test

integration:
strategy:
fail-fast: false
matrix:
couchdb-image: [ 'public.ecr.aws/medic/cht-couchdb:4.3.0', 'couchdb:3.3.3' ]

name: Integration
runs-on: ubuntu-22.04
env:
COUCHDB_IMAGE: ${{ matrix.couchdb-image }}
steps:
- uses: actions/checkout@v4
- name: Use Node.js 20.x
Expand All @@ -36,6 +43,8 @@ jobs:

name: Smoke test for older node versions
runs-on: ubuntu-22.04
env:
COUCHDB_IMAGE: 'couchdb:3.3.3'
steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.version }}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pouchdb-session-authentication",
"version": "1.3.0",
"version": "1.4.0",
"description": "Plugin that forces session authentication for PouchDb http adapter",
"main": "src/index.js",
"scripts": {
Expand Down
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ const authenticate = async (db) => {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
headers.set('Accept', 'application/json');
const authString = `${db.credentials.username}:${db.credentials.password}`;
const token = Buffer.from(decodeURIComponent(encodeURIComponent(authString))).toString('base64');
headers.set('Authorization', `Basic ${token}`);

const body = JSON.stringify({ name: db.credentials.username, password: db.credentials.password});
const response = await db.originalFetch(url.toString(), { method: 'POST', headers, body });
Expand Down
1 change: 0 additions & 1 deletion test/integration/credentials-auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ describe(`integration with ${authType}`, async function () {
tempDb = getDb(tempDbName, auth, authType);

const collectLogs = await utils.getDockerContainerLogs();
await db.allDocs();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sometimes this request ended up polluting the docker logs with an extra session request, because in CI the cookie that we generated in a previous test would have expired already.

await tempDb.allDocs();

const logs = await collectLogs(100);
Expand Down
2 changes: 1 addition & 1 deletion test/integration/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
services:
couchdb:
image: couchdb:3.3.3
image: "${COUCHDB_IMAGE:-couchdb:3.3.3}"
environment:
- "COUCHDB_USER=${COUCHDB_USER:-admin}"
- "COUCHDB_PASSWORD=${COUCHDB_PASSWORD:-pass}"
Expand Down
13 changes: 8 additions & 5 deletions test/integration/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ const killSpawnedProcess = (proc) => {
};

const waitForDockerContainerLogs = (...regex) => {
const params = 'logs --follow --tail=1 couchdb';
const params = 'compose logs --follow --tail=1 couchdb';
const proc = spawn(
'docker-compose',
'docker',
params.split(' '),
{
stdio: ['ignore', 'pipe', 'pipe'],
Expand Down Expand Up @@ -64,9 +64,9 @@ const waitForDockerContainerLogs = (...regex) => {
};

const getDockerContainerLogs = (...regex) => {
const params = 'logs --follow --tail=1 couchdb';
const params = 'compose logs --follow --tail=1 couchdb';
const proc = spawn(
'docker-compose',
'docker',
params.split(' '),
{
stdio: ['ignore', 'pipe', 'pipe'],
Expand Down Expand Up @@ -169,7 +169,10 @@ const setConfig = async (section, config, value, remove = false) => {
};

const setIterations = (iterations) => setConfig('chttpd_auth', 'iterations', iterations);
const setAuthTimeout = (timeout) => setConfig('chttpd_auth', 'timeout', timeout);
const setAuthTimeout = async (timeout) => {
await setConfig('chttpd_auth', 'timeout', timeout); // couch3
await setConfig('couch_httpd_auth', 'timeout', timeout); // couch2
};

const waitForCouchdb = async () => {
// eslint-disable-next-line no-constant-condition
Expand Down
15 changes: 15 additions & 0 deletions test/unit/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -175,6 +176,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -213,6 +215,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -261,6 +264,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic dXNyMTpwYXNz',
}),
body: JSON.stringify({ name: 'usr1', password: 'pass' }),
}
Expand All @@ -276,6 +280,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic dXNyMjpwYXNz',
}),
body: JSON.stringify({ name: 'usr2', password: 'pass' }),
}
Expand Down Expand Up @@ -365,6 +370,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -408,6 +414,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -461,6 +468,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic dXNyOnBhc3M=',
}),
body: JSON.stringify({ name: 'usr', password: 'pass' }),
}
Expand All @@ -484,6 +492,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic dXNyOnBhc3M=',
}),
body: JSON.stringify({ name: 'usr', password: 'pass' }),
}
Expand Down Expand Up @@ -525,6 +534,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic dXNyOnBhc3M=',
}),
body: JSON.stringify({ name: 'usr', password: 'pass' }),
}
Expand All @@ -541,6 +551,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic dXNyOnBhc3M=',
}),
body: JSON.stringify({ name: 'usr', password: 'pass' }),
}
Expand Down Expand Up @@ -569,6 +580,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand All @@ -586,6 +598,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -634,6 +647,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down Expand Up @@ -662,6 +676,7 @@ describe('Pouchdb Session authentication plugin', () => {
headers: new Headers({
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': 'Basic YWRtaW46cGFzcw==',
}),
body: JSON.stringify({ name: 'admin', password: 'pass' }),
}
Expand Down