-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from developmentseed/feature/client-permissions
Feature/client permissions
- Loading branch information
Showing
8 changed files
with
157 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const db = require('../../db') | ||
|
||
/** | ||
* client:delete | ||
* | ||
* To delete a client, an authenticated user must own this client | ||
* | ||
* | ||
* @param uid | ||
* @returns {undefined} | ||
*/ | ||
async function deleteClient (uid, { id }) { | ||
let conn = await db() | ||
const [client] = await conn('hydra_client').where('id', id) | ||
return (client.owner === uid) | ||
} | ||
|
||
module.exports = deleteClient |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const test = require('ava') | ||
const db = require('../../db') | ||
const path = require('path') | ||
const hydra = require('../../lib/hydra') | ||
const sinon = require('sinon') | ||
|
||
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') | ||
|
||
let agent | ||
test.before(async () => { | ||
const conn = await db() | ||
await conn.migrate.latest({ directory: migrationsDirectory }) | ||
|
||
// seed | ||
await conn('users').insert({ id: 100 }) | ||
|
||
// stub hydra introspect | ||
let introspectStub = sinon.stub(hydra, 'introspect') | ||
introspectStub.withArgs('validToken').returns({ | ||
active: true, | ||
sub: '100' | ||
}) | ||
introspectStub.withArgs('invalidToken').returns({ active: false }) | ||
|
||
// stub hydra get clients | ||
let getClientsStub = sinon.stub('hydra', 'getClients') | ||
getClientsStub.returns([]) | ||
|
||
agent = require('supertest').agent(await require('../../index')()) | ||
}) | ||
|
||
test.after.always(async () => { | ||
const conn = await db() | ||
await conn.migrate.rollback({ directory: migrationsDirectory }) | ||
conn.destroy() | ||
}) | ||
|
||
test('an authenticated user can view their clients', async t => { | ||
let res = await agent.get('/api/clients') | ||
.set('Authorization', `Bearer validToken`) | ||
|
||
t.is(res.status, 200) | ||
}) | ||
|
||
test('an unauthenticated user cannot view their clients', async t => { | ||
let res = await agent.get('/api/clients') | ||
|
||
t.is(res.status, 401) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const test = require('ava') | ||
const db = require('../../db') | ||
const path = require('path') | ||
const hydra = require('../../lib/hydra') | ||
const sinon = require('sinon') | ||
|
||
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') | ||
|
||
let agent | ||
test.before(async () => { | ||
const conn = await db() | ||
await conn.migrate.latest({ directory: migrationsDirectory }) | ||
await conn.schema.createTable('hydra_client', t => { | ||
// schema at https://github.com/ory/hydra/blob/master/client/manager_sql.go | ||
t.string('id') | ||
t.string('owner') | ||
}) | ||
|
||
// seed | ||
await conn('hydra_client').insert({ id: 999, owner: '100' }) | ||
await conn('hydra_client').insert({ id: 998, owner: '101' }) | ||
|
||
// stub hydra introspect | ||
let introspectStub = sinon.stub(hydra, 'introspect') | ||
introspectStub.withArgs('validToken').returns({ | ||
active: true, | ||
sub: '100' | ||
}) | ||
introspectStub.withArgs('differentUser').returns({ | ||
active: true, | ||
sub: '101' | ||
}) | ||
introspectStub.withArgs('invalidToken').returns({ active: false }) | ||
|
||
// stub hydra delete client | ||
let deleteClientStub = sinon.stub(hydra, 'deleteClient') | ||
deleteClientStub.returns(Promise.resolve(true)) | ||
|
||
agent = require('supertest').agent(await require('../../index')()) | ||
}) | ||
|
||
test.after.always(async () => { | ||
const conn = await db() | ||
await conn.schema.dropTable('hydra_client') | ||
await conn.migrate.rollback({ directory: migrationsDirectory }) | ||
conn.destroy() | ||
}) | ||
|
||
test('a user can delete a client they created', async t => { | ||
let res = await agent.delete('/api/clients/999') | ||
.set('Authorization', 'Bearer validToken') | ||
|
||
t.is(res.status, 200) | ||
}) | ||
|
||
test("a user can't delete a client they don't own", async t => { | ||
let res = await agent.delete('/api/clients/998') | ||
.set('Authorization', 'Bearer validToken') | ||
|
||
t.is(res.status, 401) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters