Skip to content
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: Channel and Group #kick APIs #39

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
* [/api/v1/channels.info](docs/channels.md#channelsinfo)
* [/api/v1/channels.invite](docs/channels.md#channelsinvite)
* [/api/v1/channels.join](docs/channels.md#channelsjoin)
* [/api/v1/channels.kick](docs/channels.md#channelskick)
* [/api/v1/channels.leave](docs/channels.md#channelsleave)
* [/api/v1/channels.list](docs/channels.md#channelslist)
* [/api/v1/channels.rename](docs/channels.md#channelsrename)
Expand All @@ -82,6 +83,7 @@ This gem supports the following Rocket.Chat APIs (Tested against Rocket.Chat v0.
* [/api/v1/groups.removeModerator](docs/groups.md#groupsremovemoderator)
* /api/v1/groups.info
* /api/v1/groups.invite
* [/api/v1/groups.kick](docs/groups.md#groupskick)
* /api/v1/groups.leave
* [/api/v1/groups.list](docs/groups.md#groupslist)
* /api/v1/groups.rename
Expand Down
9 changes: 9 additions & 0 deletions docs/channels.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,15 @@ channel = session.channels.join(name: 'some_channel_name')

Either room_id (RocketChat's ID) or name can be used.

#### channels.kick

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
channel = session.channels.kick(room_id: 'some-room-id', user_id: 'some-user-id')
```

#### channels.leave

Expand Down
12 changes: 11 additions & 1 deletion docs/groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
session.groups.members(name: 'some_channel_name')

```
```

#### groups.kick

```ruby
require 'rocketchat'

rocket_server = RocketChat::Server.new('http://your.server.address/')
session = rocket_server.login('username', 'password')
channel = session.groups.kick(room_id: 'some-room-id', user_id: 'some-user-id')
```
15 changes: 15 additions & 0 deletions lib/rocket_chat/messages/channel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ def join(room_id: nil, name: nil)
)['success']
end

#
# channels.kick REST API
# @param [String] room_id Rocket.Chat room id
# @param [String] user_id Rocket.Chat user id
# @return [Boolean]
# @raise [HTTPError, StatusError]
#
def kick(room_id: nil, user_id: nil)
session.request_json(
'/api/v1/channels.kick',
method: :post,
body: room_params(room_id, nil).merge(user_params(user_id, nil))
)['success']
end

#
# channels.list REST API
# @param [Integer] offset Query offset
Expand Down
15 changes: 15 additions & 0 deletions lib/rocket_chat/messages/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ def remove_leader(room_id: nil, name: nil, user_id: nil, username: nil)
)['success']
end

#
# groups.kick REST API
# @param [String] room_id Rocket.Chat room id
# @param [String] user_id Rocket.Chat user id
# @return [Boolean]
# @raise [HTTPError, StatusError]
#
def kick(room_id: nil, user_id: nil)
session.request_json(
'/api/v1/groups.kick',
method: :post,
body: room_params(room_id, nil).merge(user_params(user_id, nil))
)['success']
end

# groups.list REST API
# @param [Integer] offset Query offset
# @param [Integer] count Query count/limit
Expand Down
47 changes: 47 additions & 0 deletions spec/rocket_chat/messages/channel_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,51 @@
end
end
end

describe '#kick' do
before do
# Stubs for /api/v1/channels.kick REST API
stub_unauthed_request :post, '/api/v1/channels.kick'

stub_authed_request(:post, '/api/v1/channels.kick')
.to_return(not_provided_room_body)

stub_authed_request(:post, '/api/v1/channels.kick')
.with(
body: { roomId: 'missing-room', userId: '1' }
).to_return(invalid_room_body)

stub_authed_request(:post, '/api/v1/channels.kick')
.with(
body: { roomId: 'a-room', userId: '1' }
).to_return(
body: { success: true }.to_json,
status: 200
)
end

context 'with a valid session' do
it 'returns success' do
expect(scope.kick(room_id: 'a-room', user_id: '1')).to be_truthy
end

context 'with a missing room' do
it 'raises a status error' do
expect do
scope.kick(room_id: 'missing-room', user_id: '1')
end.to raise_error RocketChat::StatusError, invalid_room_message
end
end
end

context 'with an invalid session token' do
let(:token) { RocketChat::Token.new(authToken: nil, roomId: nil) }

it 'raises a status error' do
expect do
scope.kick(room_id: 'a-room', user_id: 'test')
end.to raise_error RocketChat::StatusError, 'You must be logged in to do this.'
end
end
end
end
47 changes: 47 additions & 0 deletions spec/rocket_chat/messages/group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,51 @@
end
end
end

describe '#kick' do
before do
# Stubs for /api/v1/groups.kick REST API
stub_unauthed_request :post, '/api/v1/groups.kick'

stub_authed_request(:post, '/api/v1/groups.kick')
.to_return(not_provided_room_body)

stub_authed_request(:post, '/api/v1/groups.kick')
.with(
body: { roomId: 'missing-room', userId: '1' }
).to_return(invalid_room_body)

stub_authed_request(:post, '/api/v1/groups.kick')
.with(
body: { roomId: 'a-room', userId: '1' }
).to_return(
body: { success: true }.to_json,
status: 200
)
end

context 'with a valid session' do
it 'returns success' do
expect(scope.kick(room_id: 'a-room', user_id: '1')).to be_truthy
end

context 'with a missing room' do
it 'raises a status error' do
expect do
scope.kick(room_id: 'missing-room', user_id: '1')
end.to raise_error RocketChat::StatusError, invalid_room_message
end
end
end

context 'with an invalid session token' do
let(:token) { RocketChat::Token.new(authToken: nil, roomId: nil) }

it 'raises a status error' do
expect do
scope.kick(room_id: 'a-room', user_id: 'test')
end.to raise_error RocketChat::StatusError, 'You must be logged in to do this.'
end
end
end
end