-
Notifications
You must be signed in to change notification settings - Fork 243
/
UserUIInteraction.cs
77 lines (65 loc) · 2.91 KB
/
UserUIInteraction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using SlackAPI.Tests.Configuration;
using SlackAPI.Tests.Helpers;
using Xunit;
namespace SlackAPI.Tests
{
// Run UI tests on a single plateform to avoid Slack Captcha
// (captcha is displayed when trying to login too often)
#if NETFRAMEWORK
[Collection("Integration tests")]
public class UserUIInteraction
{
private readonly IntegrationFixture fixture;
public UserUIInteraction(IntegrationFixture fixture)
{
this.fixture = fixture;
}
[Fact]
public void TestGetAccessToken()
{
var clientId = this.fixture.Config.ClientId;
var clientSecret = this.fixture.Config.ClientSecret;
var redirectUrl = this.fixture.Config.RedirectUrl;
var authUsername = this.fixture.Config.AuthUsername;
var authPassword = this.fixture.Config.AuthPassword;
var authWorkspace = this.fixture.Config.AuthWorkspace;
using (var driver = new ChromeDriver(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)))
{
driver.Navigate().GoToUrl($"https://{authWorkspace}.slack.com");
// Wait a bit to ensure we can properly fill username and password fields
Thread.Sleep(1000);
driver.FindElement(By.Id("email")).SendKeys(authUsername);
driver.FindElement(By.Id("password")).SendKeys(authPassword);
driver.FindElement(By.Id("signin_btn")).Click();
var slackClientHelpers = new SlackClientHelpers();
var uri = slackClientHelpers.GetAuthorizeUri(clientId, SlackScope.Identify);
driver.Navigate().GoToUrl(uri);
driver.FindElement(By.CssSelector("button[type='submit']")).Click();
var code = Regex.Match(driver.Url, "code=(?<code>[^&]+)&state").Groups["code"].Value;
var accessTokenResponse = GetAccessToken(slackClientHelpers, clientId, clientSecret, redirectUrl, code);
Assert.True(accessTokenResponse.ok);
Assert.Contains("identify", accessTokenResponse.scope);
}
}
private AccessTokenResponse GetAccessToken(SlackClientHelpers slackClientHelpers, string clientId, string clientSecret, string redirectUri, string authCode)
{
AccessTokenResponse accessTokenResponse = null;
using (var sync = new InSync(nameof(slackClientHelpers.GetAccessToken)))
{
slackClientHelpers.GetAccessToken(response =>
{
accessTokenResponse = response;
sync.Proceed();
}, clientId, clientSecret, redirectUri, authCode);
}
return accessTokenResponse;
}
}
#endif
}