/api/user/organizations/delete-membership (DELETE)
await global.api.user.organizations.DeleteMembership.delete(req) Located in Organizations module API
Returns boolean
Exceptions
These exceptions are thrown (NodeJS) or returned as JSON (HTTP) if you provide incorrect data or do not meet the requirements:
Exception | Circumstances |
---|---|
invalid querystring membershipid | |
invalid-membershipid | missing querystring membershipid |
invalid-account | accessing account is not membership or organization owner |
NodeJS source (edit on github)
If you see a problem with the source submit a pull request on Github.
const organizations = require('../../../../../index.js')
module.exports = {
delete: async (req) => {
if (!req.query || !req.query.membershipid) {
throw new Error('invalid-membershipid')
}
const membership = await global.api.user.organizations.Membership.get(req)
if (!membership) {
throw new Error('invalid-membershipid')
}
req.query.organizationid = membership.organizationid
const organization = await global.api.user.organizations.Organization.get(req)
if (!organization) {
throw new Error('invalid-organizationid')
}
await organizations.Storage.delete(`${req.appid}/membership/${req.query.membershipid}`)
await organizations.StorageList.remove(`${req.appid}/memberships`, req.query.membershipid)
await organizations.StorageList.remove(`${req.appid}/account/memberships/${membership.accountid}`, req.query.membershipid)
await organizations.StorageList.remove(`${req.appid}/organization/memberships/${organization.organizationid}`, req.query.membershipid)
await organizations.Storage.delete(`${req.appid}/map/organizationid/membershipid/${membership.accountid}/${req.query.organizationid}`)
return true
}
}
Test source (edit on github)
Tests perform real HTTP requests against a running Dashboard server.
/* eslint-env mocha */
const assert = require('assert')
const TestHelper = require('../../../../../test-helper.js')
describe('/api/user/organizations/delete-membership', () => {
describe('exceptions', () => {
describe('invalid-membershipid', () => {
it('missing querystring membershipid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/delete-membership')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-membershipid')
})
it('invalid querystring membershipid', async () => {
const user = await TestHelper.createUser()
const req = TestHelper.createRequest('/api/user/organizations/delete-membership?membershipid=invalid')
req.account = user.account
req.session = user.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-membershipid')
})
})
describe('invalid-account', () => {
it('accessing account is not membership or organization owner', async () => {
const owner = await TestHelper.createUser()
const user = await TestHelper.createUser()
global.userProfileFields = ['display-name', 'display-email']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user, owner)
global.userProfileFields = ['contact-email', 'full-name']
const user2 = await TestHelper.createUser()
const req = TestHelper.createRequest(`/api/user/organizations/delete-membership?membershipid=${user.membership.membershipid}`)
req.account = user2.account
req.session = user2.session
let errorMessage
try {
await req.delete()
} catch (error) {
errorMessage = error.message
}
assert.strictEqual(errorMessage, 'invalid-account')
})
})
})
describe('returns', () => {
it('boolean', async () => {
const owner = await TestHelper.createUser()
const user = await TestHelper.createUser()
global.userProfileFields = ['display-email', 'display-name']
await TestHelper.createProfile(owner, {
'display-name': owner.profile.firstName,
'display-email': owner.profile.contactEmail
})
await TestHelper.createProfile(user, {
'display-name': user.profile.firstName,
'display-email': user.profile.contactEmail
})
await TestHelper.createOrganization(owner, {
email: owner.profile.displayEmail,
name: 'My organization',
profileid: owner.profile.profileid
})
await TestHelper.createInvitation(owner)
await TestHelper.acceptInvitation(user, owner)
const req = TestHelper.createRequest(`/api/user/organizations/delete-membership?membershipid=${user.membership.membershipid}`)
req.account = user.account
req.session = user.session
req.filename = __filename
req.saveResponse = true
const deleted = await req.delete()
assert.strictEqual(deleted, true)
})
})
})