Skip to content

A platform for community discussion. Free, open, simple.

3.2 KB
Raw Blame
# frozen_string_literal: true

describe "Admin Chat Incoming Webhooks" do
  fab!(:current_user, :admin)
  fab!(:chat_channel_1, :chat_channel)

  let(:dialog) { PageObjects::Components::Dialog.new }
  let(:admin_incoming_webhooks_page) { PageObjects::Pages::AdminIncomingWebhooks.new }
  let(:d_page_header) { PageObjects::Components::DPageHeader.new }

  before do
    chat_system_bootstrap(current_user)
    sign_in(current_user)
  end

  it "can filter channels in the channel chooser" do
    other_channel = Fabricate(:chat_channel)
    Fabricate.times(9, :chat_channel)

    admin_incoming_webhooks_page.visit
    admin_incoming_webhooks_page.click_new

    chooser = admin_incoming_webhooks_page.channel_chooser
    chooser.expand
    chooser.search(chat_channel_1.title)

    expect(chooser).to have_no_option_value(other_channel.id)
    expect(chooser).to have_option_value(chat_channel_1.id)
  end

  it "can create incoming webhooks" do
    admin_incoming_webhooks_page.visit

    expect(d_page_header).to be_visible

    admin_incoming_webhooks_page.click_new

    expect(d_page_header).to be_hidden

    admin_incoming_webhooks_page.form.field("name").fill_in("Test webhook")
    admin_incoming_webhooks_page.form.field("description").fill_in("Some test content")
    admin_incoming_webhooks_page.form.field("username").fill_in("system")
    admin_incoming_webhooks_page.channel_chooser.expand
    admin_incoming_webhooks_page.channel_chooser.select_row_by_value(chat_channel_1.id)
    admin_incoming_webhooks_page.channel_chooser.collapse
    # TODO (martin) Add an emoji selection once Joffrey's emoji selector
    # unification has landed in core.

    admin_incoming_webhooks_page.form.submit

    expect(page).to have_content(I18n.t("js.chat.incoming_webhooks.created"))
    expect(page).to have_content(Chat::IncomingWebhook.find_by(name: "Test webhook").url)
  end

  describe "existing webhooks" do
    fab!(:webhook_1, :incoming_chat_webhook)
    fab!(:webhook_2, :incoming_chat_webhook)

    it "can list existing incoming webhooks" do
      admin_incoming_webhooks_page.visit
      expect(page).to have_content(webhook_1.name)
      expect(page).to have_content(webhook_1.chat_channel.title)
      expect(page).to have_content(webhook_2.name)
      expect(page).to have_content(webhook_2.chat_channel.title)
    end

    it "can edit an existing incoming webhook" do
      admin_incoming_webhooks_page.visit
      admin_incoming_webhooks_page
        .list_row(webhook_1.id)
        .find(".admin-chat-incoming-webhooks-edit")
        .click
      expect(admin_incoming_webhooks_page.form.field("name").value).to eq(webhook_1.name)
      admin_incoming_webhooks_page.form.field("name").fill_in("Wow so cool")
      admin_incoming_webhooks_page.form.submit
      expect(page).to have_content(I18n.t("js.chat.incoming_webhooks.saved"))
      admin_incoming_webhooks_page.visit
      expect(page).to have_content("Wow so cool")
    end

    it "can delete an existing incoming webhook" do
      admin_incoming_webhooks_page.visit
      admin_incoming_webhooks_page
        .list_row(webhook_1.id)
        .find(".admin-chat-incoming-webhooks-delete")
        .click
      dialog.click_danger
      expect(page).not_to have_content(webhook_1.name)
    end
  end
end