Skip to content

Commit

Permalink
Add unit test for public access
Browse files Browse the repository at this point in the history
  • Loading branch information
xbgmsharp committed Nov 2, 2023
1 parent 30de9b7 commit b6ef06d
Showing 1 changed file with 187 additions and 0 deletions.
187 changes: 187 additions & 0 deletions tests/index5.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
"use strict";
/*
* Unit test #5
* Public/Anonymous access
*
* process.env.PGSAIL_API_URI = from inside the docker
*
* npm install supertest should mocha mochawesome moment
* alias mocha="./node_modules/mocha/bin/_mocha"
* mocha index5.js --reporter mochawesome --reporter-options reportDir=/mnt/postgsail/,reportFilename=report_api.html
*
*/

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

const supertest = require("supertest");
// Deprecated
const should = require("should");
//const chai = require("chai");
//const should = chai.should();
let request = null;
var moment = require("moment");

// Users Array
[
{
cname: process.env.PGSAIL_API_URI,
name: "PostgSail unit test kapla",
logs: {
url: "/logs_view",
header: { name: "x-is-public", value: btoa("1,public_logs_list") },
payload: null,
res: {},
},
log: {
url: "/log_view?id=eq.1",
header: { name: "x-is-public", value: btoa("2,public_logs") },
payload: null,
res: {},
},
monitoring: {
url: "/monitoring_view",
header: { name: "x-is-public", value: btoa("1,public_monitoring") },
payload: null,
res: {},
},
timelapse: {
url: "/rpc/timelapse_fn",
header: { name: "x-is-public", value: btoa("1,public_timelapse") },
payload: null,
res: {},
},
export_gpx: {
url: "/rpc/export_logbook_gpx_fn",
header: { name: "x-is-public", value: btoa("1,public_logs") },
payload: null,
res: {},
},
},
{
cname: process.env.PGSAIL_API_URI,
name: "PostgSail unit test, aava",
logs: {
url: "/logs_view",
header: { name: "x-is-public", value: btoa("2,public_logs_list") },
payload: null,
res: {},
},
log: {
url: "/log_view?id=eq.1",
header: { name: "x-is-public", value: btoa("3,public_logs") },
payload: null,
res: {},
},
monitoring: {
url: "/monitoring_view",
header: { name: "x-is-public", value: btoa("2,public_monitoring") },
payload: null,
res: {},
},
timelapse: {
url: "/rpc/timelapse_fn",
header: { name: "x-is-public", value: btoa("2,public_timelapse") },
payload: null,
res: {},
},
export_gpx: {
url: "/rpc/export_logbook_gpx_fn",
header: { name: "x-is-public", value: btoa("2,public_logs") },
payload: null,
res: {},
},
},
].forEach(function (test) {
//console.log(`${test.cname}`);
describe(`${test.name}`, function () {
request = supertest.agent(test.cname);
request.set("User-Agent", "PostgSail unit tests");

describe("Get JWT api_anonymous", function () {
it("/logs_view, api_anonymous no jwt token", function (done) {
// Reset agent so we do not save cookies
request = supertest.agent(test.cname);
request
.get(test.logs.url)
.set(test.logs.header.name, test.logs.header.value)
.set("Accept", "application/json")
.end(function (err, res) {
res.status.should.equal(401);
should.exist(res.header["content-type"]);
should.exist(res.header["server"]);
res.header["content-type"].should.match(new RegExp("json", "g"));
res.header["server"].should.match(new RegExp("postgrest", "g"));
done(err);
});
});
it("/log_view, api_anonymous no jwt token", function (done) {
// Reset agent so we do not save cookies
request = supertest.agent(test.cname);
request
.get(test.log.url)
.set(test.log.header.name, test.log.header.value)
.set("Accept", "application/json")
.end(function (err, res) {
res.status.should.equal(200);
should.exist(res.header["content-type"]);
should.exist(res.header["server"]);
res.header["content-type"].should.match(new RegExp("json", "g"));
res.header["server"].should.match(new RegExp("postgrest", "g"));
done(err);
});
});
it("/monitoring_view, api_anonymous no jwt token", function (done) {
// Reset agent so we do not save cookies
request = supertest.agent(test.cname);
request
.get(test.monitoring.url)
.set(test.monitoring.header.name, test.monitoring.header.value)
.set("Accept", "application/json")
.end(function (err, res) {
console.log(res.text);
res.status.should.equal(200);
should.exist(res.header["content-type"]);
should.exist(res.header["server"]);
res.header["content-type"].should.match(new RegExp("json", "g"));
res.header["server"].should.match(new RegExp("postgrest", "g"));
done(err);
});
});
it("/rpc/timelapse_fn, api_anonymous no jwt token", function (done) {
// Reset agent so we do not save cookies
request = supertest.agent(test.cname);
request
.post(test.timelapse.url)
.set(test.timelapse.header.name, test.timelapse.header.value)
.set("Accept", "application/json")
.end(function (err, res) {
console.log(res.text);
res.status.should.equal(401);
should.exist(res.header["content-type"]);
should.exist(res.header["server"]);
res.header["content-type"].should.match(new RegExp("json", "g"));
res.header["server"].should.match(new RegExp("postgrest", "g"));
done(err);
});
});
it("/rpc/export_logbook_gpx_fn, api_anonymous no jwt token", function (done) {
// Reset agent so we do not save cookies
request = supertest.agent(test.cname);
request
.post(test.export_gpx.url)
.send({_id: 1})
.set(test.export_gpx.header.name, test.export_gpx.header.value)
.set("Accept", "application/json")
.end(function (err, res) {
console.log(res.text)
res.status.should.equal(401);
should.exist(res.header["content-type"]);
should.exist(res.header["server"]);
res.header["content-type"].should.match(new RegExp("json", "g"));
res.header["server"].should.match(new RegExp("postgrest", "g"));
done(err);
});
});
}); // user JWT
}); // OpenAPI description
}); // Users Array

0 comments on commit b6ef06d

Please sign in to comment.