mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-27 09:35:39 -05:00
Private clans server updates
Only return private clans in lists to owners. Get for specific private clan still allowed. Restrict create/join private clan to premium users.
This commit is contained in:
parent
6d892359c7
commit
bc35a27750
3 changed files with 383 additions and 234 deletions
|
@ -17,13 +17,15 @@ ClanHandler = class ClanHandler extends Handler
|
||||||
|
|
||||||
hasAccess: (req) ->
|
hasAccess: (req) ->
|
||||||
return true if req.method in ['GET']
|
return true if req.method in ['GET']
|
||||||
return true if req.user? and not req.user.isAnonymous()
|
return false unless req.user?
|
||||||
|
return false if req.user.isAnonymous()
|
||||||
|
return true if req.body.type is 'public' or req.user.isPremium()
|
||||||
false
|
false
|
||||||
|
|
||||||
hasAccessToDocument: (req, document, method=null) ->
|
hasAccessToDocument: (req, document, method=null) ->
|
||||||
return false unless document?
|
return false unless document?
|
||||||
method = (method or req.method).toLowerCase()
|
|
||||||
return true if req.user?.isAdmin()
|
return true if req.user?.isAdmin()
|
||||||
|
method = (method or req.method).toLowerCase()
|
||||||
return true if method is 'get'
|
return true if method is 'get'
|
||||||
return true if document.get('ownerID')?.equals req.user._id
|
return true if document.get('ownerID')?.equals req.user._id
|
||||||
false
|
false
|
||||||
|
@ -64,12 +66,17 @@ ClanHandler = class ClanHandler extends Handler
|
||||||
clanID = mongoose.Types.ObjectId(clanID)
|
clanID = mongoose.Types.ObjectId(clanID)
|
||||||
catch err
|
catch err
|
||||||
return @sendNotFoundError(res, err)
|
return @sendNotFoundError(res, err)
|
||||||
Clan.update {_id: clanID}, {$addToSet: {members: req.user._id}}, (err) =>
|
Clan.findById clanID, (err, clan) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
User.update {_id: req.user._id}, {$addToSet: {clans: clanID}}, (err) =>
|
return @sendDatabaseError(res, err) unless clan
|
||||||
|
return @sendDatabaseError(res, err) unless clanType = clan.get('type')
|
||||||
|
return @sendForbiddenError(res) unless clanType is 'public' or req.user.isPremium()
|
||||||
|
Clan.update {_id: clanID}, {$addToSet: {members: req.user._id}}, (err) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
@sendSuccess(res)
|
User.update {_id: req.user._id}, {$addToSet: {clans: clanID}}, (err) =>
|
||||||
AnalyticsLogEvent.logEvent req.user, 'Clan joined', clanID: clanID, type: 'public'
|
return @sendDatabaseError(res, err) if err
|
||||||
|
@sendSuccess(res)
|
||||||
|
AnalyticsLogEvent.logEvent req.user, 'Clan joined', clanID: clanID, type: clanType
|
||||||
|
|
||||||
leaveClan: (req, res, clanID) ->
|
leaveClan: (req, res, clanID) ->
|
||||||
return @sendForbiddenError(res) unless req.user? and not req.user.isAnonymous()
|
return @sendForbiddenError(res) unless req.user? and not req.user.isAnonymous()
|
||||||
|
|
|
@ -543,7 +543,9 @@ UserHandler = class UserHandler extends Handler
|
||||||
@getDocumentForIdOrSlug userIDOrSlug, (err, user) =>
|
@getDocumentForIdOrSlug userIDOrSlug, (err, user) =>
|
||||||
return @sendNotFoundError(res) if not user
|
return @sendNotFoundError(res) if not user
|
||||||
clanIDs = user.get('clans') ? []
|
clanIDs = user.get('clans') ? []
|
||||||
Clan.find {_id: {$in: clanIDs}}, (err, documents) =>
|
query = {$and: [{_id: {$in: clanIDs}}]}
|
||||||
|
query['$and'].push {type: 'public'} unless req.user.id is user.id
|
||||||
|
Clan.find query, (err, documents) =>
|
||||||
return @sendDatabaseError(res, err) if err
|
return @sendDatabaseError(res, err) if err
|
||||||
@sendSuccess(res, documents)
|
@sendSuccess(res, documents)
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ mongoose = require 'mongoose'
|
||||||
describe 'Clans', ->
|
describe 'Clans', ->
|
||||||
stripe = require('stripe')(config.stripe.secretKey)
|
stripe = require('stripe')(config.stripe.secretKey)
|
||||||
clanURL = getURL('/db/clan')
|
clanURL = getURL('/db/clan')
|
||||||
|
userURL = getURL('/db/user')
|
||||||
|
|
||||||
clanCount = 0
|
clanCount = 0
|
||||||
createClanName = (name) -> name + clanCount++
|
createClanName = (name) -> name + clanCount++
|
||||||
|
@ -41,269 +42,408 @@ describe 'Clans', ->
|
||||||
throw err if err
|
throw err if err
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Create clan', (done) ->
|
describe 'Public', ->
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', 'test description', (clan) ->
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Anonymous create clan 401', (done) ->
|
it 'Create clan', (done) ->
|
||||||
logoutUser ->
|
loginNewUser (user1) ->
|
||||||
requestBody =
|
createClan user1, 'public', 'test description', (clan) ->
|
||||||
type: 'public'
|
done()
|
||||||
name: createClanName 'myclan'
|
|
||||||
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(401)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Create clan missing type 422', (done) ->
|
it 'Anonymous create clan 401', (done) ->
|
||||||
loginNewUser (user1) ->
|
logoutUser ->
|
||||||
requestBody =
|
requestBody =
|
||||||
name: createClanName 'myclan'
|
type: 'public'
|
||||||
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
name: createClanName 'myclan'
|
||||||
expect(err).toBeNull()
|
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
||||||
expect(res.statusCode).toBe(422)
|
expect(err).toBeNull()
|
||||||
done()
|
expect(res.statusCode).toBe(401)
|
||||||
|
done()
|
||||||
|
|
||||||
it 'Create clan missing name 422', (done) ->
|
it 'Create clan missing type 403', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
requestBody =
|
requestBody =
|
||||||
type: 'public'
|
name: createClanName 'myclan'
|
||||||
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(422)
|
expect(res.statusCode).toBe(403)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Get public clans', (done) ->
|
it 'Create clan missing name 422', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan1) ->
|
requestBody =
|
||||||
createClan user1, 'public', 'the second clan', (clan2) ->
|
type: 'public'
|
||||||
request.get {uri: "#{clanURL}/-/public" }, (err, res, body) ->
|
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(422)
|
||||||
expect(body.length).toBeGreaterThan(1)
|
done()
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Get public clans anonymous', (done) ->
|
it 'Get public clans', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan1) ->
|
createClan user1, 'public', null, (clan1) ->
|
||||||
createClan user1, 'public', null, (clan2) ->
|
createClan user1, 'public', 'the second clan', (clan2) ->
|
||||||
logoutUser ->
|
|
||||||
request.get {uri: "#{clanURL}/-/public" }, (err, res, body) ->
|
request.get {uri: "#{clanURL}/-/public" }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
expect(body.length).toBeGreaterThan(1)
|
expect(body.length).toBeGreaterThan(1)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Join clan', (done) ->
|
it 'Get public clans anonymous', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan1) ->
|
createClan user1, 'public', null, (clan1) ->
|
||||||
loginNewUser (user2) ->
|
createClan user1, 'public', null, (clan2) ->
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
logoutUser ->
|
||||||
expect(err).toBeNull()
|
request.get {uri: "#{clanURL}/-/public" }, (err, res, body) ->
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
Clan.findById clan1.id, (err, clan1) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(clan1.get('members')?.length).toEqual(2)
|
|
||||||
expect(_.find clan1.get('members'), (memberID) -> user2._id.equals memberID).toBeDefined()
|
|
||||||
User.findById user2.id, (err, user2) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(user2.get('clans')?.length).toBeGreaterThan(0)
|
|
||||||
expect(_.find user2.get('clans'), (clanID) -> clan1._id.equals clanID).toBeDefined()
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Join invalid clan 404', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
request.put {uri: "#{clanURL}/1234/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(404)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Join clan anonymous 401', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
logoutUser ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(401)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Join clan twice 200', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
Clan.findById clan1.id, (err, clan1) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(_.find clan1.get('members'), (memberID) -> memberID.equals user2.id).toBeDefined()
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
|
expect(body.length).toBeGreaterThan(1)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Leave clan', (done) ->
|
it 'Join clan', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', 'do not stay too long', (clan1) ->
|
createClan user1, 'public', null, (clan1) ->
|
||||||
loginNewUser (user2) ->
|
loginNewUser (user2) ->
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
|
Clan.findById clan1.id, (err, clan1) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(clan1.get('members')?.length).toEqual(2)
|
||||||
|
expect(_.find clan1.get('members'), (memberID) -> user2._id.equals memberID).toBeDefined()
|
||||||
|
User.findById user2.id, (err, user2) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(user2.get('clans')?.length).toBeGreaterThan(0)
|
||||||
|
expect(_.find user2.get('clans'), (clanID) -> clan1._id.equals clanID).toBeDefined()
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Join invalid clan 404', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.put {uri: "#{clanURL}/1234/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(404)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Join clan anonymous 401', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
logoutUser ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(401)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Join clan twice 200', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
Clan.findById clan1.id, (err, clan1) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(_.find clan1.get('members'), (memberID) -> memberID.equals user2.id).toBeDefined()
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Leave clan', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', 'do not stay too long', (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/leave" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
Clan.findById clan1.id, (err, clan1) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(_.find clan1.get('members'), (memberID) -> memberID.equals user2.id).toBeUndefined()
|
||||||
|
User.findById user2.id, (err, user2) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(user2.get('clans').length).toEqual(0)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Leave clan not member 200', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/leave" }, (err, res, body) ->
|
request.put {uri: "#{clanURL}/#{clan1.id}/leave" }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(200)
|
||||||
Clan.findById clan1.id, (err, clan1) ->
|
Clan.findById clan1.id, (err, clan1) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(_.find clan1.get('members'), (memberID) -> memberID.equals user2.id).toBeUndefined()
|
expect(_.find clan1.get('members'), (memberID) -> memberID.equals user2.id).toBeUndefined()
|
||||||
User.findById user2.id, (err, user2) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(user2.get('clans').length).toEqual(0)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Leave clan not member 200', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/leave" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
Clan.findById clan1.id, (err, clan1) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(_.find clan1.get('members'), (memberID) -> memberID.equals user2.id).toBeUndefined()
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Leave owned clan 403', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/leave" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(403)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Remove member', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
loginUser user1, (user1) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
Clan.findById clan1.id, (err, clan1) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(clan1.get('members').length).toEqual(1)
|
|
||||||
expect(clan1.get('members')[0]).toEqual(user1.get('_id'))
|
|
||||||
User.findById user2.id, (err, user2) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(user2.get('clans').length).toEqual(0)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Remove non-member 200', (done) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
Clan.findById clan1.id, (err, clan1) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(clan1.get('members').length).toEqual(1)
|
|
||||||
expect(clan1.get('members')[0]).toEqual(user1.get('_id'))
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Remove invalid memberID 404', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/remove/123" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(404)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Remove member, not in clan 403', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan1) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(200)
|
|
||||||
loginNewUser (user3) ->
|
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(403)
|
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Remove member, not the owner 403', (done) ->
|
it 'Leave owned clan 403', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan1) ->
|
createClan user1, 'public', null, (clan1) ->
|
||||||
loginNewUser (user2) ->
|
request.put {uri: "#{clanURL}/#{clan1.id}/leave" }, (err, res, body) ->
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(res.statusCode).toBe(403)
|
||||||
loginNewUser (user3) ->
|
done()
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
|
||||||
|
it 'Remove member', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
loginUser user1, (user1) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
Clan.findById clan1.id, (err, clan1) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(clan1.get('members').length).toEqual(1)
|
||||||
|
expect(clan1.get('members')[0]).toEqual(user1.get('_id'))
|
||||||
|
User.findById user2.id, (err, user2) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(user2.get('clans').length).toEqual(0)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Remove non-member 200', (done) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
Clan.findById clan1.id, (err, clan1) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(200)
|
expect(clan1.get('members').length).toEqual(1)
|
||||||
|
expect(clan1.get('members')[0]).toEqual(user1.get('_id'))
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Remove invalid memberID 404', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/remove/123" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(404)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Remove member, not in clan 403', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
loginNewUser (user3) ->
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(403)
|
expect(res.statusCode).toBe(403)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Remove member from owned clan 403', (done) ->
|
it 'Remove member, not the owner 403', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan1) ->
|
createClan user1, 'public', null, (clan1) ->
|
||||||
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user1.id}" }, (err, res, body) ->
|
loginNewUser (user2) ->
|
||||||
expect(err).toBeNull()
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
expect(res.statusCode).toBe(403)
|
expect(err).toBeNull()
|
||||||
done()
|
expect(res.statusCode).toBe(200)
|
||||||
|
loginNewUser (user3) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user2.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(403)
|
||||||
|
done()
|
||||||
|
|
||||||
it 'Delete clan', (done) ->
|
it 'Remove member from owned clan 403', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan) ->
|
createClan user1, 'public', null, (clan1) ->
|
||||||
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
request.put {uri: "#{clanURL}/#{clan1.id}/remove/#{user1.id}" }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(204)
|
|
||||||
User.findById user1.id, (err, user1) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(user1.get('clans').length).toEqual(0)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Delete clan anonymous 401', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan) ->
|
|
||||||
logoutUser ->
|
|
||||||
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(401)
|
|
||||||
done()
|
|
||||||
|
|
||||||
it 'Delete clan not owner 403', (done) ->
|
|
||||||
loginNewUser (user1) ->
|
|
||||||
createClan user1, 'public', null, (clan) ->
|
|
||||||
loginNewUser (user2) ->
|
|
||||||
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(403)
|
expect(res.statusCode).toBe(403)
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Delete clan no longer exists 404', (done) ->
|
it 'Delete clan', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
createClan user1, 'public', null, (clan) ->
|
createClan user1, 'public', null, (clan) ->
|
||||||
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
|
||||||
expect(err).toBeNull()
|
|
||||||
expect(res.statusCode).toBe(204)
|
|
||||||
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
||||||
expect(err).toBeNull()
|
expect(err).toBeNull()
|
||||||
expect(res.statusCode).toBe(404)
|
expect(res.statusCode).toBe(204)
|
||||||
|
User.findById user1.id, (err, user1) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(user1.get('clans').length).toEqual(0)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Delete clan anonymous 401', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan) ->
|
||||||
|
logoutUser ->
|
||||||
|
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(401)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Delete clan not owner 403', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(403)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Delete clan no longer exists 404', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan) ->
|
||||||
|
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(204)
|
||||||
|
request.del {uri: "#{clanURL}/#{clan.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(404)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Delete clan invalid ID 404', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
request.del {uri: "#{clanURL}/1234" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(404)
|
||||||
|
done()
|
||||||
|
|
||||||
|
describe 'Private', ->
|
||||||
|
# Using stripe.free = true to convert users to premium
|
||||||
|
|
||||||
|
it 'Create clan', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'test description', (clan) ->
|
||||||
done()
|
done()
|
||||||
|
|
||||||
it 'Delete clan invalid ID 404', (done) ->
|
it 'Create clan when not premium 403', (done) ->
|
||||||
loginNewUser (user1) ->
|
loginNewUser (user1) ->
|
||||||
request.del {uri: "#{clanURL}/1234" }, (err, res, body) ->
|
requestBody =
|
||||||
expect(err).toBeNull()
|
type: 'private'
|
||||||
expect(res.statusCode).toBe(404)
|
name: createClanName 'myclan'
|
||||||
done()
|
request.post {uri: clanURL, json: requestBody }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(403)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Join clan', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'test description', (clan) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
user2.set 'stripe.free', true
|
||||||
|
user2.save (err) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Join clan when not premium 403', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'test description', (clan) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
user2.save (err) ->
|
||||||
|
request.put {uri: "#{clanURL}/#{clan.id}/join" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(403)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it 'Get public clans after creating a private clan', (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
createClan user1, 'public', null, (clan1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
createClan user1, 'private', 'my private clan', (clan2) ->
|
||||||
|
request.get {uri: "#{clanURL}/-/public" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
clans = JSON.parse(body)
|
||||||
|
expect(clans.length).toBeGreaterThan(1)
|
||||||
|
for clan in clans
|
||||||
|
expect(clan._id).not.toEqual(clan2.id)
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "Getting nother user's clans excludes their private ones", (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'my private clan', (clan1) ->
|
||||||
|
createClan user1, 'public', 'my public clan', (clan2) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.get {uri: "#{userURL}/#{user1.id}/clans" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
clans = JSON.parse(body)
|
||||||
|
expect(clans.length).toEqual(1)
|
||||||
|
for clan in clans
|
||||||
|
expect(clan._id).toEqual(clan2.id)
|
||||||
|
expect(clan.type).toEqual('public')
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "Getting own clans includes private ones", (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'my private clan', (clan1) ->
|
||||||
|
createClan user1, 'public', 'my public clan', (clan2) ->
|
||||||
|
request.get {uri: "#{userURL}/#{user1.id}/clans" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
clans = JSON.parse(body)
|
||||||
|
expect(clans.length).toEqual(2)
|
||||||
|
for clan in clans
|
||||||
|
if clan.type is 'public'
|
||||||
|
expect(clan._id).toEqual(clan2.id)
|
||||||
|
else
|
||||||
|
expect(clan._id).toEqual(clan1.id)
|
||||||
|
expect(clan.type).toEqual('private')
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "Can get another user's private clan", (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'my private clan', (clan1) ->
|
||||||
|
loginNewUser (user2) ->
|
||||||
|
request.get {uri: "#{clanURL}/#{clan1.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
clan = JSON.parse(body)
|
||||||
|
expect(clan._id).toEqual(clan1.id)
|
||||||
|
expect(clan.name).toEqual(clan1.get('name'))
|
||||||
|
expect(clan.type).toEqual('private')
|
||||||
|
expect(clan1.get('ownerID').equals clan.ownerID).toEqual(true)
|
||||||
|
expect(clan.description).toEqual(clan1.get('description'))
|
||||||
|
done()
|
||||||
|
|
||||||
|
it "Can get another user's private clan as anonymous", (done) ->
|
||||||
|
loginNewUser (user1) ->
|
||||||
|
user1.set 'stripe.free', true
|
||||||
|
user1.save (err) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
createClan user1, 'private', 'my private clan', (clan1) ->
|
||||||
|
logoutUser ->
|
||||||
|
request.get {uri: "#{clanURL}/#{clan1.id}" }, (err, res, body) ->
|
||||||
|
expect(err).toBeNull()
|
||||||
|
expect(res.statusCode).toBe(200)
|
||||||
|
clan = JSON.parse(body)
|
||||||
|
expect(clan._id).toEqual(clan1.id)
|
||||||
|
expect(clan.name).toEqual(clan1.get('name'))
|
||||||
|
expect(clan.type).toEqual('private')
|
||||||
|
expect(clan1.get('ownerID').equals clan.ownerID).toEqual(true)
|
||||||
|
expect(clan.description).toEqual(clan1.get('description'))
|
||||||
|
done()
|
||||||
|
|
Loading…
Reference in a new issue