-
-
Notifications
You must be signed in to change notification settings - Fork 286
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat(editor): User account deletion endpoint #843
Open
tr1ten
wants to merge
5
commits into
metabrainz:master
Choose a base branch
from
tr1ten:user-deleter
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ad8811b
feat(editor): delete user from BB project
tr1ten b16f010
test: add tests for '/delete-user' endpoint
tr1ten 6bdaaeb
use user's metabrainz id to fetch their info
tr1ten 299baac
test: add test for unauthenticated user
tr1ten 14b9e60
move superagent-config file into test directory
tr1ten File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,69 @@ | ||
import {createEditor, truncateEntities} from '../../../test-helpers/create-entities'; | ||
import app from '../../../../src/server/app'; | ||
import chai from 'chai'; | ||
import chaiHttp from 'chai-http'; | ||
import config from '../../../test-helpers/superagent-mock-config'; | ||
import mock from 'superagent-mock'; | ||
import {omit} from 'lodash'; | ||
import orm from '../../../bookbrainz-data'; | ||
import request from 'superagent'; | ||
|
||
|
||
mock(request, config); | ||
|
||
chai.use(chaiHttp); | ||
const {expect} = chai; | ||
const {Editor} = orm; | ||
|
||
const deletedUserAttrib = { | ||
areaId: null, | ||
bio: '', | ||
cachedMetabrainzName: null, | ||
genderId: null, | ||
id: 2, | ||
metabrainzUserId: null, | ||
name: 'Deleted User #2', | ||
reputation: 0, | ||
revisionsApplied: 0, | ||
revisionsReverted: 0, | ||
titleUnlockId: null, | ||
totalRevisions: 0 | ||
}; | ||
|
||
describe('delete user', () => { | ||
const adminEditorId = 1; | ||
const userEditorId = 2; | ||
const userMBId = 2; | ||
const otherEditorId = 3; | ||
let agent; | ||
before(async () => { | ||
await createEditor(userEditorId, {metabrainzUserId: userMBId}); | ||
agent = chai.request.agent(app); | ||
await agent.get('/cb'); | ||
}); | ||
|
||
after(truncateEntities); | ||
|
||
it('Normal User should not be able to delete other users', async () => { | ||
const res = await agent.post(`/delete-user/${userMBId}?access_token=${otherEditorId}`); | ||
expect(res.ok).to.be.false; | ||
// unauthorized | ||
expect(res.status).to.be.equal(401); | ||
}); | ||
|
||
it('User Deleter should be able to delete a user', async () => { | ||
const res = await agent.post(`/delete-user/${userMBId}?access_token=${adminEditorId}`); | ||
expect(res.ok).to.be.true; | ||
const editor = await new Editor({id: userEditorId}).fetch(); | ||
expect(editor).to.exist; | ||
const editorJson = omit(editor.toJSON(), ['activeAt', 'createdAt', 'typeId']); | ||
expect(editorJson).to.deep.equal(deletedUserAttrib); | ||
}); | ||
|
||
it('Unauthenticated user should not be able to delete other users', async () => { | ||
const res = await chai.request(app).post(`/delete-user/${userMBId}`); | ||
expect(res.ok).to.be.false; | ||
// unauthorized | ||
expect(res.status).to.be.equal(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable camelcase */ | ||
const mockConfig = [ | ||
{ | ||
fixtures: (match, params, headers) => { | ||
// extract auth token from headers | ||
const token = headers.Authorization.split(' ')[1]; | ||
// using user id as tokens for testing | ||
let sub; | ||
let metabrainz_user_id; | ||
switch (token) { | ||
case '1': | ||
sub = 'UserDeleter'; | ||
metabrainz_user_id = 2007538; | ||
break; | ||
case '2': | ||
sub = 'NormalUser1'; | ||
metabrainz_user_id = 2; | ||
break; | ||
case '3': | ||
sub = 'NormalUser2'; | ||
metabrainz_user_id = 3; | ||
break; | ||
default: | ||
sub = 'UnknownUser'; | ||
metabrainz_user_id = 0; | ||
break; | ||
} | ||
const body = { | ||
metabrainz_user_id, | ||
sub | ||
}; | ||
return {body}; | ||
}, | ||
get(match, data) { | ||
return data; | ||
}, | ||
pattern: 'https://musicbrainz.org/oauth2/userinfo' | ||
} | ||
]; | ||
|
||
export default mockConfig; |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd also love to see, for good measure, another test to ensure we get a 401 for an unauthenticated user.
Basically the same test as the above, but using
await chai.request(app).post(…
<- this will not use a user session because we won't call/cb
as we do in thebefore
function