Browse Source

Log into Slack

master
Dylan Baker 4 years ago
parent
commit
beb7ee7cf6
3 changed files with 68 additions and 0 deletions
  1. 3
    0
      .env.sample
  2. 41
    0
      lib/slack_mattermost_emoji/slack_client.rb
  3. 24
    0
      spec/slack_client_spec.rb

+ 3
- 0
.env.sample View File

@@ -0,0 +1,3 @@
1
+SLACK_DOMAIN=
2
+SLACK_USERNAME=
3
+SLACK_PASSWORD=

+ 41
- 0
lib/slack_mattermost_emoji/slack_client.rb View File

@@ -0,0 +1,41 @@
1
+require 'selenium-webdriver'
2
+
3
+module SlackMattermostEmoji
4
+  class SlackClient
5
+    def initialize(username:, password:, domain:)
6
+      @username = username
7
+      @password = password
8
+      @domain = domain
9
+      @authenticated = false
10
+
11
+      options = Selenium::WebDriver::Chrome::Options.new
12
+      options.headless!
13
+      @driver = Selenium::WebDriver.for :chrome, options: options
14
+
15
+
16
+      if [@username, @password, @domain].any? { |el| el.nil? || el.empty? }
17
+        raise 'Slack username, password, and domain are all required'
18
+      end
19
+    end
20
+
21
+    def authenticate
22
+      @driver.navigate.to "https://#{@domain}"
23
+      @driver.find_element(name: 'email').send_keys(@username)
24
+      @driver.find_element(name: 'password').send_keys(@password)
25
+      @driver.find_element(id: 'signin_btn').click
26
+
27
+      begin
28
+        @driver.find_element(css: 'p.alert.alert_error')
29
+        raise 'Invalid credentials'
30
+      rescue Selenium::WebDriver::Error::NoSuchElementError
31
+        nil
32
+      end
33
+
34
+      @authenticated = true
35
+    end
36
+
37
+    def authenticated?
38
+      @authenticated
39
+    end
40
+  end
41
+end

+ 24
- 0
spec/slack_client_spec.rb View File

@@ -0,0 +1,24 @@
1
+require "dotenv/load"
2
+require "slack_mattermost_emoji/slack_client"
3
+
4
+RSpec.describe SlackMattermostEmoji::SlackClient do
5
+  it 'should be able to log in to slack using the provided credentials' do
6
+    client = SlackMattermostEmoji::SlackClient.new(
7
+      username: ENV['SLACK_USERNAME'],
8
+      password: ENV['SLACK_PASSWORD'],
9
+      domain: ENV['SLACK_DOMAIN']
10
+    )
11
+    client.authenticate
12
+
13
+    expect(client.authenticated?).to be true
14
+  end
15
+
16
+  it 'should handle a bad credentials error' do
17
+    client = SlackMattermostEmoji::SlackClient.new(
18
+      username: ENV['SLACK_USERNAME'],
19
+      password: 'wrongpassword',
20
+      domain: ENV['SLACK_DOMAIN']
21
+    )
22
+    expect { client.authenticate }.to raise_error('Invalid credentials')
23
+  end
24
+end

Loading…
Cancel
Save