-
Notifications
You must be signed in to change notification settings - Fork 13.7k
[FIX] channels.convertToTeam endpoint doesn't work when only the channelName param is provided
#26858
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
[FIX] channels.convertToTeam endpoint doesn't work when only the channelName param is provided
#26858
Changes from 14 commits
811a951
1d13bd4
09f0b1b
105444f
f8997ad
0bf369b
fb24acf
2133c0f
d7df939
0f1ddd6
35605b4
1879101
1e14d94
a52871d
36210b0
971f37c
80fa434
aaebe90
7400c8b
ad9a658
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1922,7 +1922,7 @@ describe('[Channels]', function () { | |
| }); | ||
| }); | ||
|
|
||
| it('should successfully convert a channel to a team', (done) => { | ||
| it(`should successfully convert a channel to a team when the channel's id is sent as parameter`, (done) => { | ||
| updatePermission('create-team', ['admin']).then(() => { | ||
| updatePermission('edit-room', ['admin']).then(() => { | ||
| request | ||
|
|
@@ -1938,6 +1938,62 @@ describe('[Channels]', function () { | |
| }); | ||
| }); | ||
|
|
||
| it(`should return an error when the channel's name and id are sent as parameter`, (done) => { | ||
| updatePermission('create-team', ['admin']).then(() => { | ||
| updatePermission('edit-room', ['admin']).then(() => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no need to update permissions.. looking the previous test, this is the current state already. |
||
| request | ||
| .post(api('teams.convertToChannel')) | ||
| .set(credentials) | ||
| .send({ teamName: this.newChannel.name }) | ||
| .expect(200) | ||
| .expect((res) => { | ||
| expect(res.body).to.have.a.property('success', true); | ||
| }) | ||
| .then(() => { | ||
| request | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this Promise nesting and the previous one with the permissions update is wrong in so many levels 🙈 I see this has been the "standard" on this particular file, but we shouldn't propagate bad code even if it is the current "standard".. this is conceptually wrong since the updatePermission()
.then(() => updatePermission())
.then(() => request
.post(api('teams.convertToChannel'))
.set(credentials)
// ....
).then(() => request
.post(api('teams.convertToTeam'))
.set(credentials)
// ....
)as you can see, everything is done in a single level of nesting. BUT, you should also not do this, since |
||
| .post(api('channels.convertToTeam')) | ||
| .set(credentials) | ||
| .send({ | ||
| channelName: this.newChannel.name, | ||
| channelId: this.newChannel._id, | ||
| }) | ||
| .expect(400) | ||
| .expect((res) => { | ||
| expect(res.body).to.have.property('success', false); | ||
| expect(res.body).to.have.property('error').include(`must match exactly one schema in oneOf`); | ||
| }) | ||
| .end(done); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it(`should successfully convert a channel to a team when the channel's name is sent as parameter`, (done) => { | ||
| updatePermission('create-team', ['admin']).then(() => { | ||
| updatePermission('edit-room', ['admin']).then(() => { | ||
| request | ||
| .post(api('teams.convertToChannel')) | ||
| .set(credentials) | ||
| .send({ teamName: this.newChannel.name }) | ||
| .expect(200) | ||
| .expect((res) => { | ||
| expect(res.body).to.have.a.property('success', true); | ||
| }) | ||
| .then(() => { | ||
| request | ||
| .post(api('channels.convertToTeam')) | ||
| .set(credentials) | ||
| .send({ channelName: this.newChannel.name }) | ||
| .expect(200) | ||
| .expect((res) => { | ||
| expect(res.body).to.have.a.property('success', true); | ||
| }) | ||
| .end(done); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| it('should fail to convert channel without the required parameters', (done) => { | ||
| request.post(api('channels.convertToTeam')).set(credentials).send({}).expect(400).end(done); | ||
| }); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Do we have to do it like this?
In this particular context,
roomId in paramsis the same asparams.roomId ?(the purpose of it) - so no matter what you do in the calling function, the called still has to check for both values any way.This conditional and destructuring just seems useless at that point. And IMO is bad. We should keep the code simple. And inspire others (people who are gonna read your code) to do so too.
on the called, you can simply change the param type from the union to a simple all-partial and chain the truthness checks instead of membership checks as it was before
roomId ? .. : roomName ? ... : undefinedit's late for me, hopefully i was able to explain myself.
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.
Hey Debdut, I'll first explain what was the issue on the way we were calling this function (that is, with
params: { roomId: channelId, roomName: channelName }): this would cause one of the fields in theparamsobject to beundefined. So checking'roomId' in paramsinfindChannelByIdOrNamewould always returntrue(since this field is in the object, even though its value isundefined).Still, I see we could change the params type to
params: { roomId?: string; roomName?: string }and check forparams.roomIdinstead of'roomId' in params(is that what you mean?). If we do so, I think we should also bring back the error check we had before (so as to throw an error when both params are undefined).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.
Already done by the route function. So, you can safely ignore that part.
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.
Rocket.Chat/apps/meteor/app/api/server/v1/channels.ts
Lines 464 to 466 in aaebe90