Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
Rocket.Chat
Manage
Activity
Members
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package registry
Model registry
Operate
Terraform modules
Analyze
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
RocketChat
Rocket.Chat
Commits
99141498
Commit
99141498
authored
6 years ago
by
Marcos Spessatto Defendi
Committed by
Diego Sampaio
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[NEW] Add /users.deleteOwnAccount REST endpoint to an user delete his own account (#11488)
parent
92f60c0c
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
packages/rocketchat-api/server/v1/users.js
+18
-0
18 additions, 0 deletions
packages/rocketchat-api/server/v1/users.js
tests/end-to-end/api/01-users.js
+132
-0
132 additions, 0 deletions
tests/end-to-end/api/01-users.js
with
150 additions
and
0 deletions
packages/rocketchat-api/server/v1/users.js
+
18
−
0
View file @
99141498
...
...
@@ -59,6 +59,24 @@ RocketChat.API.v1.addRoute('users.delete', { authRequired: true }, {
}
});
RocketChat
.
API
.
v1
.
addRoute
(
'
users.deleteOwnAccount
'
,
{
authRequired
:
true
},
{
post
()
{
const
{
password
}
=
this
.
bodyParams
;
if
(
!
password
)
{
return
RocketChat
.
API
.
v1
.
failure
(
'
Body parameter "password" is required.
'
);
}
if
(
!
RocketChat
.
settings
.
get
(
'
Accounts_AllowDeleteOwnAccount
'
))
{
throw
new
Meteor
.
Error
(
'
error-not-allowed
'
,
'
Not allowed
'
);
}
Meteor
.
runAsUser
(
this
.
userId
,
()
=>
{
Meteor
.
call
(
'
deleteUserOwnAccount
'
,
password
);
});
return
RocketChat
.
API
.
v1
.
success
();
}
});
RocketChat
.
API
.
v1
.
addRoute
(
'
users.getAvatar
'
,
{
authRequired
:
false
},
{
get
()
{
const
user
=
this
.
getUserFromParams
();
...
...
This diff is collapsed.
Click to expand it.
tests/end-to-end/api/01-users.js
+
132
−
0
View file @
99141498
...
...
@@ -664,4 +664,136 @@ describe('[Users]', function() {
});
});
describe
(
'
[/users.deleteOwnAccount]
'
,
()
=>
{
const
testUsername
=
`testuser
${
+
new
Date
()
}
`
;
let
targetUser
;
let
userCredentials
;
it
(
'
register a new user...
'
,
(
done
)
=>
{
request
.
post
(
api
(
'
users.register
'
))
.
set
(
credentials
)
.
send
({
email
:
`
${
testUsername
}
.@teste.com`
,
username
:
`
${
testUsername
}
test`
,
name
:
testUsername
,
pass
:
password
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
targetUser
=
res
.
body
.
user
;
})
.
end
(
done
);
});
it
(
'
Login...
'
,
(
done
)
=>
{
request
.
post
(
api
(
'
login
'
))
.
send
({
user
:
targetUser
.
username
,
password
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
userCredentials
=
{};
userCredentials
[
'
X-Auth-Token
'
]
=
res
.
body
.
data
.
authToken
;
userCredentials
[
'
X-User-Id
'
]
=
res
.
body
.
data
.
userId
;
})
.
end
(
done
);
});
it
(
'
Enable "Accounts_AllowDeleteOwnAccount" setting...
'
,
(
done
)
=>
{
request
.
post
(
'
/api/v1/settings/Accounts_AllowDeleteOwnAccount
'
)
.
set
(
credentials
)
.
send
({
'
value
'
:
true
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
expect
(
res
.
body
).
to
.
have
.
property
(
'
success
'
,
true
);
})
.
end
(
done
);
});
it
(
'
should delete user own account
'
,
(
done
)
=>
{
request
.
post
(
api
(
'
users.deleteOwnAccount
'
))
.
set
(
userCredentials
)
.
send
({
password
:
crypto
.
createHash
(
'
sha256
'
).
update
(
password
,
'
utf8
'
).
digest
(
'
hex
'
)
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
expect
(
res
.
body
).
to
.
have
.
property
(
'
success
'
,
true
);
})
.
end
(
done
);
});
});
describe
(
'
[/users.delete]
'
,
()
=>
{
const
updatePermission
=
(
permission
,
roles
)
=>
{
return
new
Promise
(
resolve
=>
{
request
.
post
(
api
(
'
permissions.update
'
))
.
set
(
credentials
)
.
send
({
permissions
:
[{
_id
:
permission
,
roles
}]
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
expect
(
res
.
body
).
to
.
have
.
property
(
'
success
'
,
true
);
})
.
end
(
resolve
);
});
};
const
testUsername
=
`testuser
${
+
new
Date
()
}
`
;
let
targetUser
;
it
(
'
register a new user...
'
,
(
done
)
=>
{
request
.
post
(
api
(
'
users.register
'
))
.
set
(
credentials
)
.
send
({
email
:
`
${
testUsername
}
.@teste.com`
,
username
:
`
${
testUsername
}
test`
,
name
:
testUsername
,
pass
:
password
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
targetUser
=
res
.
body
.
user
;
})
.
end
(
done
);
});
it
(
'
should return an error when trying delete user account without "delete-user" permission
'
,
(
done
)
=>
{
updatePermission
(
'
delete-user
'
,
[
'
user
'
])
.
then
(()
=>
{
request
.
post
(
api
(
'
users.delete
'
))
.
set
(
credentials
)
.
send
({
userId
:
targetUser
.
_id
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
403
)
.
expect
((
res
)
=>
{
expect
(
res
.
body
).
to
.
have
.
property
(
'
success
'
,
false
);
expect
(
res
.
body
).
to
.
have
.
property
(
'
error
'
,
'
unauthorized
'
);
})
.
end
(
done
);
});
});
it
(
'
should delete user account when logged user has "delete-user" permission
'
,
(
done
)
=>
{
updatePermission
(
'
delete-user
'
,
[
'
admin
'
])
.
then
(()
=>
{
request
.
post
(
api
(
'
users.delete
'
))
.
set
(
credentials
)
.
send
({
userId
:
targetUser
.
_id
})
.
expect
(
'
Content-Type
'
,
'
application/json
'
)
.
expect
(
200
)
.
expect
((
res
)
=>
{
expect
(
res
.
body
).
to
.
have
.
property
(
'
success
'
,
true
);
})
.
end
(
done
);
});
});
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment