-
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 #90 from developmentseed/join-public-teams-#80
authenticated users can join public teams
- Loading branch information
Showing
7 changed files
with
172 additions
and
14 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 { isPublic, isMember } = require('../../lib/team') | ||
|
||
/** | ||
* team:join | ||
* | ||
* To join a team, the team must be public | ||
* | ||
* @param {string} uid user id | ||
* @param {Object} params request parameters | ||
* @returns {boolean} can the request go through? | ||
*/ | ||
async function joinTeam (uid, { id }) { | ||
const publicTeam = await isPublic(id) | ||
const member = await isMember(id, uid) | ||
return publicTeam && !member | ||
} | ||
|
||
module.exports = joinTeam |
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,72 @@ | ||
const test = require('ava') | ||
const db = require('../../db') | ||
const path = require('path') | ||
const hydra = require('../../lib/hydra') | ||
const sinon = require('sinon') | ||
|
||
const team = require('../../lib/team') | ||
|
||
const migrationsDirectory = path.join(__dirname, '..', '..', 'db', 'migrations') | ||
|
||
let agent | ||
test.before(async (t) => { | ||
const conn = await db() | ||
await conn.migrate.latest({ directory: migrationsDirectory }) | ||
|
||
// seed | ||
await conn('users').insert({ id: 100 }) | ||
await conn('users').insert({ id: 101 }) | ||
|
||
t.context.publicTeam = await team.create({ name: 'public team' }, 100) | ||
t.context.privateTeam = await team.create({ name: 'private team', privacy: 'private' }, 100) | ||
|
||
// 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 }) | ||
|
||
agent = require('supertest').agent(await require('../../index')()) | ||
}) | ||
|
||
test.after.always(async () => { | ||
const conn = await db() | ||
await conn.migrate.rollback({ directory: migrationsDirectory }) | ||
conn.destroy() | ||
}) | ||
|
||
test('a user can join a public team', async t => { | ||
const team = t.context.publicTeam | ||
let res = await agent.put(`/api/teams/${team.id}/join`) | ||
.set('Authorization', `Bearer differentUser`) | ||
t.is(res.status, 200) | ||
}) | ||
|
||
test('a user cannot join a private team', async t => { | ||
const team = t.context.privateTeam | ||
let res = await agent.put(`/api/teams/${team.id}/join`) | ||
.set('Authorization', `Bearer differentUser`) | ||
t.is(res.status, 401) | ||
}) | ||
|
||
test('a user cannot join a team they are already in', async t => { | ||
const team = t.context.publicTeam | ||
let res = await agent.put(`/api/teams/${team.id}/join`) | ||
.set('Authorization', `Bearer validToken`) | ||
t.is(res.status, 401) | ||
}) | ||
|
||
test('a user must be authenticated to join a team', async t => { | ||
const team = t.context.publicTeam | ||
let invalidToken = await agent.put(`/api/teams/${team.id}/join`) | ||
.set('Authorization', `Bearer invalidToken`) | ||
t.is(invalidToken.status, 401) | ||
let unauthenticated = await agent.put(`/api/teams/${team.id}/join`) | ||
t.is(unauthenticated.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