|
127 | 127 | end |
128 | 128 | it 'returns a useful error when team already exists' do |
129 | 129 | expect_any_instance_of(Team).to receive(:ping_if_active!) |
130 | | - existing_team = Fabricate(:team, token: 'access_token') |
| 130 | + existing_team = Fabricate(:team, token: 'access_token', oauth_scope: 'original-scope', activated_user_id: 'original-user') |
131 | 131 | expect { client.teams._post(code: 'code') }.to raise_error Faraday::ClientError do |e| |
132 | 132 | json = JSON.parse(e.response[:body]) |
133 | 133 | expect(json['message']).to eq "Team #{existing_team.name} is already registered." |
134 | 134 | end |
| 135 | + expect(existing_team.reload.oauth_scope).to eq 'original-scope' |
| 136 | + expect(existing_team.activated_user_id).to eq 'original-user' |
135 | 137 | end |
136 | 138 | it 'reactivates a deactivated team with a different code' do |
137 | 139 | expect(SlackRubyBotServer::Service.instance).to receive(:start!) |
|
151 | 153 | expect(team.bot_user_id).to be nil |
152 | 154 | end.to_not change(Team, :count) |
153 | 155 | end |
| 156 | + |
| 157 | + context 'with active reinstallation enabled' do |
| 158 | + before do |
| 159 | + SlackRubyBotServer.config.allow_reinstall = true |
| 160 | + allow_any_instance_of(Team).to receive(:ping_if_active!) |
| 161 | + end |
| 162 | + |
| 163 | + it 'refreshes credentials without creating a record or starting another worker' do |
| 164 | + existing_team = Fabricate(:team, token: 'old-token', team_id: 'team_id', oauth_version: 'v2') |
| 165 | + expect(SlackRubyBotServer::Service.instance).not_to receive(:start!) |
| 166 | + expect(SlackRubyBotServer::Service.instance).not_to receive(:create!) |
| 167 | + expect(SlackRubyBotServer::Service.instance).to receive(:reinstall!).with( |
| 168 | + instance_of(Team), { state: 'application-state' } |
| 169 | + ).and_call_original |
| 170 | + |
| 171 | + expect do |
| 172 | + response = client.teams._post(code: 'code', state: 'application-state') |
| 173 | + expect(response.id.to_s).to eq existing_team.id.to_s |
| 174 | + end.not_to change(Team, :count) |
| 175 | + existing_team.reload |
| 176 | + expect(existing_team.token).to eq 'access_token' |
| 177 | + expect(existing_team.active?).to be true |
| 178 | + expect(existing_team.oauth_scope).to eq 'commands,incoming-webhook' |
| 179 | + expect(existing_team.activated_user_access_token).to eq 'access_token' |
| 180 | + expect(existing_team.activated_user_id).to eq 'user_id' |
| 181 | + end |
| 182 | + |
| 183 | + it 'does not bypass the active legacy installation guard' do |
| 184 | + existing_team = Fabricate(:team, token: 'old-token', team_id: 'team_id', oauth_version: 'v1', oauth_scope: 'original-scope') |
| 185 | + expect(SlackRubyBotServer::Service.instance).not_to receive(:reinstall!) |
| 186 | + expect { client.teams._post(code: 'code') }.to raise_error(Faraday::ClientError) |
| 187 | + existing_team.reload |
| 188 | + expect(existing_team.token).to eq 'old-token' |
| 189 | + expect(existing_team.oauth_version).to eq 'v1' |
| 190 | + expect(existing_team.oauth_scope).to eq 'original-scope' |
| 191 | + end |
| 192 | + |
| 193 | + it 'still starts newly created installations normally' do |
| 194 | + expect(SlackRubyBotServer::Service.instance).to receive(:start!).and_call_original |
| 195 | + expect(SlackRubyBotServer::Service.instance).not_to receive(:reinstall!) |
| 196 | + expect { client.teams._post(code: 'code') }.to change(Team, :count).by(1) |
| 197 | + end |
| 198 | + |
| 199 | + it 'still starts previously deactivated installations normally' do |
| 200 | + existing_team = Fabricate(:team, token: 'old-token', team_id: 'team_id', oauth_version: 'v2', active: false) |
| 201 | + expect(SlackRubyBotServer::Service.instance).to receive(:start!).and_call_original |
| 202 | + expect(SlackRubyBotServer::Service.instance).not_to receive(:reinstall!) |
| 203 | + client.teams._post(code: 'code') |
| 204 | + expect(existing_team.reload.token).to eq 'access_token' |
| 205 | + expect(existing_team.active?).to be true |
| 206 | + end |
| 207 | + end |
154 | 208 | end |
155 | 209 |
|
156 | 210 | context 'register a bot via oauth v1' do |
|
235 | 289 | end.to_not change(Team, :count) |
236 | 290 | end |
237 | 291 | it 'returns a useful error when team already exists' do |
| 292 | + SlackRubyBotServer.config.allow_reinstall = true |
238 | 293 | expect_any_instance_of(Team).to receive(:ping_if_active!) |
239 | | - existing_team = Fabricate(:team, token: 'access_token') |
| 294 | + existing_team = Fabricate(:team, token: 'access_token', oauth_version: 'v2') |
240 | 295 | expect { client.teams._post(code: 'code') }.to raise_error Faraday::ClientError do |e| |
241 | 296 | json = JSON.parse(e.response[:body]) |
242 | 297 | expect(json['message']).to eq "Team #{existing_team.name} is already registered." |
|
0 commit comments