|
1 | 1 | require 'spec_helper' |
2 | 2 |
|
3 | 3 | describe Team do |
| 4 | + context 'workspace metadata' do |
| 5 | + let(:team) { Fabricate(:team, name: 'Old name', domain: 'old-domain') } |
| 6 | + let(:auth) do |
| 7 | + Slack::Messages::Message.new( |
| 8 | + 'ok' => true, |
| 9 | + 'team_id' => team.team_id, |
| 10 | + 'team' => 'New workspace name', |
| 11 | + 'url' => 'https://new-domain.slack.com/', |
| 12 | + 'user_id' => 'UBOT' |
| 13 | + ) |
| 14 | + end |
| 15 | + let(:slack_client) { instance_double(Slack::Web::Client) } |
| 16 | + let(:presence) { { 'presence' => 'active' } } |
| 17 | + |
| 18 | + before do |
| 19 | + allow(Slack::Web::Client).to receive(:new).with(token: team.token).and_return(slack_client) |
| 20 | + allow(slack_client).to receive(:auth_test).and_return(auth) |
| 21 | + allow(slack_client).to receive(:users_getPresence).with(user: 'UBOT').and_return(presence) |
| 22 | + end |
| 23 | + |
| 24 | + it 'persists current workspace info from a ping without an extra API request' do |
| 25 | + expect(slack_client).to receive(:auth_test).once.and_return(auth) |
| 26 | + expect(team.ping!).to eq(auth: auth, presence: presence) |
| 27 | + expect(team.reload.name).to eq 'New workspace name' |
| 28 | + expect(team.domain).to eq 'new-domain' |
| 29 | + end |
| 30 | + |
| 31 | + it 'can refresh workspace info without fetching presence' do |
| 32 | + expect(slack_client).not_to receive(:users_getPresence) |
| 33 | + expect(team.update_info!).to eq auth |
| 34 | + expect(team.reload.name).to eq 'New workspace name' |
| 35 | + expect(team.domain).to eq 'new-domain' |
| 36 | + end |
| 37 | + |
| 38 | + it 'does not write an unchanged workspace' do |
| 39 | + team.update!(name: 'New workspace name', domain: 'new-domain') |
| 40 | + expect(team).not_to receive(:update!) |
| 41 | + team.ping! |
| 42 | + end |
| 43 | + |
| 44 | + it 'refreshes info even when presence requires an additional scope' do |
| 45 | + allow(slack_client).to receive(:users_getPresence).and_raise(Slack::Web::Api::Errors::MissingScope, 'missing_scope') |
| 46 | + expect(team.ping!).to eq(auth: auth, presence: nil) |
| 47 | + expect(team.reload.domain).to eq 'new-domain' |
| 48 | + end |
| 49 | + |
| 50 | + it 'does not change stored info when Slack authentication fails' do |
| 51 | + allow(slack_client).to receive(:auth_test).and_raise(Slack::Web::Api::Errors::SlackError, 'invalid_auth') |
| 52 | + expect { team.ping! }.to raise_error(Slack::Web::Api::Errors::SlackError, 'invalid_auth') |
| 53 | + expect(team.reload.name).to eq 'Old name' |
| 54 | + expect(team.domain).to eq 'old-domain' |
| 55 | + end |
| 56 | + |
| 57 | + it 'rejects a response for another workspace without changing stored info' do |
| 58 | + expect do |
| 59 | + team.update_info!(auth.merge('team_id' => 'TOTHER')) |
| 60 | + end.to raise_error(ArgumentError, 'Slack authentication belongs to a different workspace.') |
| 61 | + expect(team.reload.name).to eq 'Old name' |
| 62 | + expect(team.domain).to eq 'old-domain' |
| 63 | + end |
| 64 | + |
| 65 | + it 'rejects malformed workspace URLs without partially updating the name' do |
| 66 | + expect do |
| 67 | + team.update_info!(auth.merge('url' => '/not-a-workspace')) |
| 68 | + end.to raise_error(ArgumentError, 'Slack authentication returned an invalid workspace URL.') |
| 69 | + expect(team.reload.name).to eq 'Old name' |
| 70 | + expect(team.domain).to eq 'old-domain' |
| 71 | + end |
| 72 | + end |
| 73 | + |
4 | 74 | context '#purge!' do |
5 | 75 | let!(:active_team) { Fabricate(:team) } |
6 | 76 | let!(:inactive_team) { Fabricate(:team, active: false) } |
|
0 commit comments