<commit_before>RSpec.describe 'SlackService' do
  let(:team) { create(:team) }
  let(:team_with_slack) { create(:team, :with_slack) }
  let(:user) { create(:user) }
  let(:user_with_slack_id) { create(:user, :with_slack_id) }

  def create_add_post_command(receivers, message, amount)
    command = ""
    receivers.each { |user| command += "<@#{user.slack_id}|#{user.name}>" }
    return command += "'#{message}' #{amount}"
  end

  before :each do
    team.add_member(user)
    team.add_member(user_with_slack_id)

    team_with_slack.add_member(user)
    team_with_slack.add_member(user_with_slack_id)
  end

  describe 'add to workspace' do
    it 'updates the team channel id, access token and slack id' do
      allow_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage)
      mock_response = {
          :incoming_webhook => {
              :channel_id => 'channelId'
          },
          :access_token => 'accessToken',
          :team => {
              :id => 'teamId'
          }
      }

      allow_any_instance_of(Slack::Web::Client).to receive(:oauth_v2_access).and_return(mock_response.as_json)

      expect {
        SlackService.add_to_workspace('token', team.id)
        team.reload
      }.to change(team, :channel_id).from(nil).to('channelId')
               .and change(team, :slack_bot_access_token).from(nil).to('accessToken')
                        .and change(team, :slack_team_id).from(nil).to('teamId')
    end

    it 'returns an error if there is no token' do
      expect { SlackService.add_to_workspace(nil, team.id) }.to raise_exception(SlackService::InvalidRequest)
    end

    it 'returns an error if there is no team id' do
      expect { SlackService.add_to_workspace('token', nil) }.to raise_exception(SlackService::InvalidRequest)
    end
  end

  describe 'connect account' do
    it 'updates the slack id and registration token' do
      expect {
        SlackService.connect_account(user.slack_registration_token, 'slackId')
        user.reload
      }.to change(user, :slack_id).from(nil).to('slackId')
               .and change(user, :slack_registration_token).from(user.slack_registration_token).to(nil)
    end

    it 'raises an error if there is no token' do
      expect {
        SlackService.connect_account('', 'slackId')
      }.to raise_exception(SlackService::InvalidCommand, 'please provide a register token')
    end

    it 'raises an error if the slack id is already connected to a user' do
      expect {
        SlackService.connect_account(user.slack_registration_token,
                                     user_with_slack_id.slack_id)
      }.to raise_exception(SlackService::InvalidCommand, 'This slack account is already linked to kudo-o-matic')
    end

    it 'returns an error if there is no user for the provided registration token' do
      expect {
        SlackService.connect_account('invalidRegisterToken',
                                     'slackId')
      }.to raise_exception(SlackService::InvalidCommand, 'Invalid registration token')
    end

    it 'returns an error if the user is already connected to slack' do
      expect {
        SlackService.connect_account(user_with_slack_id.slack_registration_token,
                                     'otherSlackId')
      }.to raise_exception(SlackService::InvalidCommand, 'This kudo-o-matic account is already linked to Slack.')
    end
  end

  describe 'send post announcement' do
    it 'has the correct message' do
      post = create(:post, sender: user, receivers: [user_with_slack_id], team: team, kudos_meter: team.active_kudos_meter)

      text = "#{user.name} just gave #{post.amount} kudos to <@#{user_with_slack_id.slack_id}> for #{post.message}"

      expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(channel: nil, text: text)
      SlackService.send_post_announcement(post)
    end

    it 'posts to the correct channel' do
      team.channel_id = 'slackChannelId'
      post = create(:post, sender: user, receivers: [user_with_slack_id], team: team, kudos_meter: team.active_kudos_meter)

      expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(channel: 'slackChannelId', text: anything)
      SlackService.send_post_announcement(post)
    end
  end

  describe 'create post' do
    describe 'receivers' do
      it 'returns an error if one or more receivers are not connected to slack' do<commit_msg>Fix tests<commit_after>RSpec.describe 'SlackService' do
  let(:team) { create(:team) }
  let(:team_with_slack) { create(:team, :with_slack) }
  let(:user) { create(:user) }
  let(:user_with_slack_id) { create(:user, :with_slack_id) }

  def create_add_post_command(receivers, message, amount)
    command = ""
    receivers.each { |user| command += "<@#{user.slack_id}|#{user.name}>" }
    return command += "'#{message}' #{amount}"
  end

  before :each do
    team.add_member(user)
    team.add_member(user_with_slack_id)

    team_with_slack.add_member(user)
    team_with_slack.add_member(user_with_slack_id)
  end

  describe 'add to workspace' do
    it 'updates the team channel id, access token and slack id' do
      allow_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage)
      mock_response = {
          :incoming_webhook => {
              :channel_id => 'channelId'
          },
          :access_token => 'accessToken',
          :team => {
              :id => 'teamId'
          }
      }

      allow_any_instance_of(Slack::Web::Client).to receive(:oauth_v2_access).and_return(mock_response.as_json)

      expect {
        SlackService.add_to_workspace('token', team.id)
        team.reload
      }.to change(team, :channel_id).from(nil).to('channelId')
               .and change(team, :slack_bot_access_token).from(nil).to('accessToken')
                        .and change(team, :slack_team_id).from(nil).to('teamId')
    end

    it 'returns an error if there is no token' do
      expect { SlackService.add_to_workspace(nil, team.id) }.to raise_exception(SlackService::InvalidRequest)
    end

    it 'returns an error if there is no team id' do
      expect { SlackService.add_to_workspace('token', nil) }.to raise_exception(SlackService::InvalidRequest)
    end
  end

  describe 'connect account' do
    it 'updates the slack id and registration token' do
      expect {
        SlackService.connect_account(user.slack_registration_token, 'slackId')
        user.reload
      }.to change(user, :slack_id).from(nil).to('slackId')
               .and change(user, :slack_registration_token).from(user.slack_registration_token).to(nil)
    end

    it 'raises an error if there is no token' do
      expect {
        SlackService.connect_account('', 'slackId')
      }.to raise_exception(SlackService::InvalidCommand, 'please provide a register token')
    end

    it 'raises an error if the slack id is already connected to a user' do
      expect {
        SlackService.connect_account(user.slack_registration_token,
                                     user_with_slack_id.slack_id)
      }.to raise_exception(SlackService::InvalidCommand, 'This slack account is already linked to kudo-o-matic')
    end

    it 'returns an error if there is no user for the provided registration token' do
      expect {
        SlackService.connect_account('invalidRegisterToken',
                                     'slackId')
      }.to raise_exception(SlackService::InvalidCommand, 'Invalid registration token')
    end

    it 'returns an error if the user is already connected to slack' do
      expect {
        SlackService.connect_account(user_with_slack_id.slack_registration_token,
                                     'otherSlackId')
      }.to raise_exception(SlackService::InvalidCommand, 'This kudo-o-matic account is already linked to Slack')
    end
  end

  describe 'send post announcement' do
    it 'has the correct message' do
      post = create(:post, sender: user, receivers: [user_with_slack_id], team: team, kudos_meter: team.active_kudos_meter)

      text = "#{user.name} just gave #{post.amount} kudos to <@#{user_with_slack_id.slack_id}> for #{post.message}"

      expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(channel: nil, text: text)
      SlackService.send_post_announcement(post)
    end

    it 'posts to the correct channel' do
      team.channel_id = 'slackChannelId'
      post = create(:post, sender: user, receivers: [user_with_slack_id], team: team, kudos_meter: team.active_kudos_meter)

      expect_any_instance_of(Slack::Web::Client).to receive(:chat_postMessage).with(channel: 'slackChannelId', text: anything)
      SlackService.send_post_announcement(post)
    end
  end

  describe 'create post' do
    describe 'receivers' do
      it 'returns an error if one or more receivers are not connected to slack' do
{'fraction_non_alphanumeric': 0.10410308579179382, 'fraction_numerical': 0.00022606533288120268, 'mean_word_length': 3.167687235044748, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1540174', 'n_tokens_mistral': 2807, 'n_tokens_neox': 2716, 'n_words': 652}