|
45 | 45 | end |
46 | 46 | end |
47 | 47 |
|
48 | | - context 'register a bot' do |
| 48 | + context 'register a bot via oauth v2' do |
49 | 49 | before do |
50 | | - oauth_access = { |
51 | | - 'access_token' => 'access_token', |
52 | | - 'user_id' => 'user_id', |
53 | | - 'team_id' => 'team_id', |
54 | | - 'team_name' => 'team_name' |
55 | | - } |
| 50 | + SlackRubyBotServer.config.oauth_version = :v2 |
| 51 | + oauth_access = Slack::Messages::Message.new({ |
| 52 | + 'access_token' => 'access_token', |
| 53 | + 'authed_user' => { 'id' => 'user_id' }, |
| 54 | + 'team' => { 'id' => 'team_id', 'name' => 'team_name' } |
| 55 | + }) |
| 56 | + ENV['SLACK_CLIENT_ID'] = 'client_id' |
| 57 | + ENV['SLACK_CLIENT_SECRET'] = 'client_secret' |
| 58 | + allow_any_instance_of(Slack::Web::Client).to receive(:oauth_v2_access).with( |
| 59 | + hash_including( |
| 60 | + code: 'code', |
| 61 | + client_id: 'client_id', |
| 62 | + client_secret: 'client_secret' |
| 63 | + ) |
| 64 | + ).and_return(oauth_access) |
| 65 | + end |
| 66 | + after do |
| 67 | + ENV.delete('SLACK_CLIENT_ID') |
| 68 | + ENV.delete('SLACK_CLIENT_SECRET') |
| 69 | + end |
| 70 | + it 'creates a team' do |
| 71 | + expect(SlackRubyBotServer::Service.instance).to receive(:start!) |
| 72 | + expect do |
| 73 | + team = client.teams._post(code: 'code') |
| 74 | + expect(team.team_id).to eq 'team_id' |
| 75 | + expect(team.name).to eq 'team_name' |
| 76 | + team = Team.find(team.id) |
| 77 | + expect(team.token).to eq 'access_token' |
| 78 | + expect(team.activated_user_access_token).to eq 'access_token' |
| 79 | + expect(team.activated_user_id).to eq 'user_id' |
| 80 | + expect(team.bot_user_id).to be nil |
| 81 | + end.to change(Team, :count).by(1) |
| 82 | + end |
| 83 | + |
| 84 | + it 'includes optional state parameter' do |
| 85 | + expect(SlackRubyBotServer::Service.instance).to receive(:create!).with(instance_of(Team), state: 'property') |
| 86 | + client.teams._post(code: 'code', state: 'property') |
| 87 | + end |
| 88 | + |
| 89 | + it 'reactivates a deactivated team' do |
| 90 | + expect(SlackRubyBotServer::Service.instance).to receive(:start!) |
| 91 | + existing_team = Fabricate(:team, token: 'access_token', active: false) |
| 92 | + expect do |
| 93 | + team = client.teams._post(code: 'code') |
| 94 | + expect(team.team_id).to eq existing_team.team_id |
| 95 | + expect(team.name).to eq existing_team.name |
| 96 | + expect(team.active).to be true |
| 97 | + team = Team.find(team.id) |
| 98 | + expect(team.token).to eq 'access_token' |
| 99 | + expect(team.active).to be true |
| 100 | + expect(team.activated_user_access_token).to eq 'access_token' |
| 101 | + expect(team.activated_user_id).to eq 'user_id' |
| 102 | + expect(team.bot_user_id).to be nil |
| 103 | + end.to_not change(Team, :count) |
| 104 | + end |
| 105 | + it 'reactivates a team deactivated on slack' do |
| 106 | + expect(SlackRubyBotServer::Service.instance).to receive(:start!) |
| 107 | + existing_team = Fabricate(:team, token: 'access_token') |
| 108 | + expect do |
| 109 | + expect_any_instance_of(Team).to receive(:ping!) { raise Slack::Web::Api::Errors::SlackError, 'invalid_auth' } |
| 110 | + team = client.teams._post(code: 'code') |
| 111 | + expect(team.team_id).to eq existing_team.team_id |
| 112 | + expect(team.name).to eq existing_team.name |
| 113 | + expect(team.active).to be true |
| 114 | + team = Team.find(team.id) |
| 115 | + expect(team.token).to eq 'access_token' |
| 116 | + expect(team.active).to be true |
| 117 | + expect(team.bot_user_id).to be nil |
| 118 | + expect(team.activated_user_id).to eq 'user_id' |
| 119 | + end.to_not change(Team, :count) |
| 120 | + end |
| 121 | + it 'returns a useful error when team already exists' do |
| 122 | + expect_any_instance_of(Team).to receive(:ping_if_active!) |
| 123 | + existing_team = Fabricate(:team, token: 'access_token') |
| 124 | + expect { client.teams._post(code: 'code') }.to raise_error Faraday::ClientError do |e| |
| 125 | + json = JSON.parse(e.response[:body]) |
| 126 | + expect(json['message']).to eq "Team #{existing_team.name} is already registered." |
| 127 | + end |
| 128 | + end |
| 129 | + it 'reactivates a deactivated team with a different code' do |
| 130 | + expect(SlackRubyBotServer::Service.instance).to receive(:start!) |
| 131 | + existing_team = Fabricate(:team, token: 'old', team_id: 'team_id', active: false) |
| 132 | + expect do |
| 133 | + team = client.teams._post(code: 'code') |
| 134 | + expect(team.team_id).to eq existing_team.team_id |
| 135 | + expect(team.name).to eq existing_team.name |
| 136 | + expect(team.active).to be true |
| 137 | + team = Team.find(team.id) |
| 138 | + expect(team.token).to eq 'access_token' |
| 139 | + expect(team.active).to be true |
| 140 | + expect(team.activated_user_access_token).to eq 'access_token' |
| 141 | + expect(team.activated_user_id).to eq 'user_id' |
| 142 | + expect(team.bot_user_id).to be nil |
| 143 | + end.to_not change(Team, :count) |
| 144 | + end |
| 145 | + end |
| 146 | + |
| 147 | + context 'register a bot via oauth v1' do |
| 148 | + before do |
| 149 | + SlackRubyBotServer.config.oauth_version = :v1 |
| 150 | + oauth_access = Slack::Messages::Message.new({ |
| 151 | + 'access_token' => 'access_token', |
| 152 | + 'user_id' => 'user_id', |
| 153 | + 'team_id' => 'team_id', |
| 154 | + 'team_name' => 'team_name' |
| 155 | + }) |
56 | 156 | ENV['SLACK_CLIENT_ID'] = 'client_id' |
57 | 157 | ENV['SLACK_CLIENT_SECRET'] = 'client_secret' |
58 | 158 | allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with( |
|
146 | 246 |
|
147 | 247 | context 'register a legacy bot' do |
148 | 248 | before do |
149 | | - oauth_access = { |
150 | | - 'bot' => { |
151 | | - 'bot_access_token' => 'token', |
152 | | - 'bot_user_id' => 'bot_user_id' |
153 | | - }, |
154 | | - 'access_token' => 'access_token', |
155 | | - 'user_id' => 'user_id', |
156 | | - 'team_id' => 'team_id', |
157 | | - 'team_name' => 'team_name' |
158 | | - } |
| 249 | + SlackRubyBotServer.config.oauth_version = :v1 |
| 250 | + oauth_access = Slack::Messages::Message.new({ |
| 251 | + 'bot' => { |
| 252 | + 'bot_access_token' => 'token', |
| 253 | + 'bot_user_id' => 'bot_user_id' |
| 254 | + }, |
| 255 | + 'access_token' => 'access_token', |
| 256 | + 'user_id' => 'user_id', |
| 257 | + 'team_id' => 'team_id', |
| 258 | + 'team_name' => 'team_name' |
| 259 | + }) |
159 | 260 | ENV['SLACK_CLIENT_ID'] = 'client_id' |
160 | 261 | ENV['SLACK_CLIENT_SECRET'] = 'client_secret' |
161 | 262 | allow_any_instance_of(Slack::Web::Client).to receive(:oauth_access).with( |
|
0 commit comments