|
| 1 | +import {test} from "./utils"; |
| 2 | +import couchdbProxy from "../src/index"; |
| 3 | +import request from "supertest"; |
| 4 | + |
| 5 | +test("attaches proxy headers", async function(t, couchdb, target) { |
| 6 | + t.plan(4); |
| 7 | + |
| 8 | + couchdb.use(function(req, res) { |
| 9 | + t.equals(req.get("X-Auth-CouchDB-UserName"), "test", "proxied correct user name"); |
| 10 | + t.equals(req.get("X-Auth-CouchDB-Roles"), "testrole1,testrole2", "proxied correct roles"); |
| 11 | + t.notOk(req.get("X-Auth-CouchDB-Token"), "did not proxy a token"); |
| 12 | + res.json({ foo: "bar" }); |
| 13 | + }); |
| 14 | + |
| 15 | + const proxy = couchdbProxy(function() { |
| 16 | + return { |
| 17 | + name: "test", |
| 18 | + roles: [ "testrole1", "testrole2" ] |
| 19 | + }; |
| 20 | + }, { |
| 21 | + target |
| 22 | + }); |
| 23 | + |
| 24 | + const {body} = await request(proxy).get("/").expect(200); |
| 25 | + t.equals(body.foo, "bar", "has original json content"); |
| 26 | +}); |
| 27 | + |
| 28 | +test("signs request with secret", async function(t, couchdb, target) { |
| 29 | + t.plan(3); |
| 30 | + const secret = "mysupersecret"; |
| 31 | + const token = couchdbProxy.sign("test", secret); |
| 32 | + |
| 33 | + couchdb.use(function(req, res) { |
| 34 | + t.equals(req.get("X-Auth-CouchDB-Token"), token, "proxied signed token"); |
| 35 | + res.json({ foo: "bar" }); |
| 36 | + }); |
| 37 | + |
| 38 | + const proxy = couchdbProxy(function() { |
| 39 | + return { |
| 40 | + name: "test", |
| 41 | + roles: [ "testrole1", "testrole2" ] |
| 42 | + }; |
| 43 | + }, { |
| 44 | + target, secret |
| 45 | + }); |
| 46 | + |
| 47 | + t.ok(token, "created a token"); |
| 48 | + |
| 49 | + const {body} = await request(proxy).get("/").expect(200); |
| 50 | + t.equals(body.foo, "bar", "has original json content"); |
| 51 | +}); |
| 52 | + |
| 53 | +test("injects proxy info into root response", async function(t, couchdb, target) { |
| 54 | + t.plan(2); |
| 55 | + |
| 56 | + couchdb.get("/", (req, res) => { |
| 57 | + res.json({ foo: "bar" }); |
| 58 | + }); |
| 59 | + |
| 60 | + const proxy = couchdbProxy(function() { |
| 61 | + return { |
| 62 | + name: "test", |
| 63 | + roles: [ "testrole1", "testrole2" ] |
| 64 | + }; |
| 65 | + }, { |
| 66 | + target, |
| 67 | + info: { hello: "world" } |
| 68 | + }); |
| 69 | + |
| 70 | + const {body} = await request(proxy).get("/").expect(200); |
| 71 | + t.equals(body.foo, "bar", "has original json content"); |
| 72 | + t.deepEquals(body.proxy, { hello: "world" }, "has custom info contents"); |
| 73 | +}); |
| 74 | + |
| 75 | +test("removes original session headers", async function(t, couchdb, target) { |
| 76 | + t.plan(3); |
| 77 | + |
| 78 | + couchdb.get("/", (req, res) => { |
| 79 | + t.equals(req.get("X-Auth-CouchDB-UserName"), "test", "proxied correct user name"); |
| 80 | + t.equals(req.get("X-Auth-CouchDB-Roles"), "testrole1,testrole2", "proxied correct roles"); |
| 81 | + res.json({ foo: "bar" }); |
| 82 | + }); |
| 83 | + |
| 84 | + const proxy = couchdbProxy(function() { |
| 85 | + return { |
| 86 | + name: "test", |
| 87 | + roles: [ "testrole1", "testrole2" ] |
| 88 | + }; |
| 89 | + }, { |
| 90 | + target |
| 91 | + }); |
| 92 | + |
| 93 | + const {body} = await request(proxy) |
| 94 | + .get("/") |
| 95 | + .set("X-Auth-CouchDB-UserName", "previous") |
| 96 | + .set("X-Auth-CouchDB-Roles", "prevrole1,prevrole2") |
| 97 | + .expect(200); |
| 98 | + |
| 99 | + t.equals(body.foo, "bar", "has original json content"); |
| 100 | +}); |
0 commit comments