Fixed tests
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -196,5 +196,5 @@ FakesAssemblies/
|
|||||||
*.opt
|
*.opt
|
||||||
|
|
||||||
#Custom
|
#Custom
|
||||||
Credentials.cs
|
project.lock.json
|
||||||
project.lock.json
|
/test/Discord.Net.Tests/config.json
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ namespace Discord.Helpers
|
|||||||
static Http()
|
static Http()
|
||||||
{
|
{
|
||||||
_client = new HttpClient();
|
_client = new HttpClient();
|
||||||
_client.DefaultRequestHeaders.Add("Accept", "*/*");
|
_client.DefaultRequestHeaders.Add("accept", "*/*");
|
||||||
_client.DefaultRequestHeaders.Add("Accept-language", "en-US;q=0.8");
|
_client.DefaultRequestHeaders.Add("accept-language", "en-US;q=0.8");
|
||||||
|
|
||||||
string version = typeof(Http).GetTypeInfo().Assembly.GetName().Version.ToString(2);
|
string version = typeof(Http).GetTypeInfo().Assembly.GetName().Version.ToString(2);
|
||||||
_client.DefaultRequestHeaders.Add("User-agent", $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)");
|
_client.DefaultRequestHeaders.Add("user-agent", $"Discord.Net/{version} (https://github.com/RogueException/Discord.Net)");
|
||||||
}
|
}
|
||||||
|
|
||||||
private static string _token;
|
private static string _token;
|
||||||
@@ -36,7 +36,9 @@ namespace Discord.Helpers
|
|||||||
set
|
set
|
||||||
{
|
{
|
||||||
_token = value;
|
_token = value;
|
||||||
_client.DefaultRequestHeaders.Add("Authorization", _token);
|
_client.DefaultRequestHeaders.Remove("authorization");
|
||||||
|
if (_token != null)
|
||||||
|
_client.DefaultRequestHeaders.Add("authorization", _token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -132,19 +134,6 @@ namespace Discord.Helpers
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Stream GetDecoder(string contentEncoding, MemoryStream encodedStream)
|
|
||||||
{
|
|
||||||
switch (contentEncoding)
|
|
||||||
{
|
|
||||||
case "gzip":
|
|
||||||
return new GZipStream(encodedStream, CompressionMode.Decompress, true);
|
|
||||||
case "deflate":
|
|
||||||
return new DeflateStream(encodedStream, CompressionMode.Decompress, true);
|
|
||||||
default:
|
|
||||||
throw new ArgumentOutOfRangeException("Unknown encoding: " + contentEncoding);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#if DEBUG
|
#if DEBUG
|
||||||
private static void CheckResponse<T>(string json, T obj)
|
private static void CheckResponse<T>(string json, T obj)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -26,8 +26,8 @@ namespace Discord
|
|||||||
{
|
{
|
||||||
ServerId = serverId;
|
ServerId = serverId;
|
||||||
UserId = userId;
|
UserId = userId;
|
||||||
_client = client;
|
|
||||||
JoinedAt = joinedAt;
|
JoinedAt = joinedAt;
|
||||||
}
|
_client = client;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,7 +8,7 @@
|
|||||||
<AppDesignerFolder>Properties</AppDesignerFolder>
|
<AppDesignerFolder>Properties</AppDesignerFolder>
|
||||||
<RootNamespace>Discord.Net.Tests</RootNamespace>
|
<RootNamespace>Discord.Net.Tests</RootNamespace>
|
||||||
<AssemblyName>Discord.Net.Tests</AssemblyName>
|
<AssemblyName>Discord.Net.Tests</AssemblyName>
|
||||||
<TargetFrameworkVersion>v4.6</TargetFrameworkVersion>
|
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
|
||||||
<FileAlignment>512</FileAlignment>
|
<FileAlignment>512</FileAlignment>
|
||||||
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
|
||||||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
|
||||||
@@ -36,6 +36,10 @@
|
|||||||
<WarningLevel>4</WarningLevel>
|
<WarningLevel>4</WarningLevel>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
|
||||||
|
<HintPath>..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
|
||||||
|
<Private>True</Private>
|
||||||
|
</Reference>
|
||||||
<Reference Include="System" />
|
<Reference Include="System" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<Choose>
|
<Choose>
|
||||||
@@ -52,7 +56,7 @@
|
|||||||
</Choose>
|
</Choose>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Tests.cs" />
|
<Compile Include="Tests.cs" />
|
||||||
<Compile Include="Credentials.cs" />
|
<Compile Include="Settings.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
@@ -61,6 +65,9 @@
|
|||||||
<Name>Discord.Net</Name>
|
<Name>Discord.Net</Name>
|
||||||
</ProjectReference>
|
</ProjectReference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="packages.config" />
|
||||||
|
</ItemGroup>
|
||||||
<Choose>
|
<Choose>
|
||||||
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
|
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
29
test/Discord.Net.Tests/Settings.cs
Normal file
29
test/Discord.Net.Tests/Settings.cs
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
using Newtonsoft.Json;
|
||||||
|
using System.IO;
|
||||||
|
|
||||||
|
namespace Discord.Net.Tests
|
||||||
|
{
|
||||||
|
internal class Settings
|
||||||
|
{
|
||||||
|
private const string path = "../../config.json";
|
||||||
|
public static Settings Load()
|
||||||
|
{
|
||||||
|
if (!File.Exists(path))
|
||||||
|
throw new FileNotFoundException("config.json is missing, rename config.json.example and add data for two separate unused accounts for testing.");
|
||||||
|
return JsonConvert.DeserializeObject<Settings>(File.ReadAllText(path));
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Account
|
||||||
|
{
|
||||||
|
[JsonProperty("email")]
|
||||||
|
public string Email { get; set; }
|
||||||
|
[JsonProperty("password")]
|
||||||
|
public string Password { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[JsonProperty("user1")]
|
||||||
|
public Account User1 { get; set; }
|
||||||
|
[JsonProperty("user2")]
|
||||||
|
public Account User2 { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -10,21 +10,24 @@ namespace Discord.Net.Tests
|
|||||||
public class Tests
|
public class Tests
|
||||||
{
|
{
|
||||||
private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
|
private const int EventTimeout = 5000; //Max time in milliseconds to wait for an event response from our test actions
|
||||||
private DiscordClient _bot1, _bot2;
|
|
||||||
private Server _testServer;
|
|
||||||
private Channel _testServerChannel;
|
|
||||||
private Random _random;
|
|
||||||
|
|
||||||
[TestInitialize]
|
private static Settings _settings;
|
||||||
public void Initialize()
|
private static DiscordClient _bot1, _bot2;
|
||||||
|
private static Server _testServer;
|
||||||
|
private static Channel _testServerChannel;
|
||||||
|
private static Random _random;
|
||||||
|
|
||||||
|
[ClassInitialize]
|
||||||
|
public static void Initialize(TestContext testContext)
|
||||||
{
|
{
|
||||||
|
_settings = Settings.Load();
|
||||||
_random = new Random();
|
_random = new Random();
|
||||||
|
|
||||||
_bot1 = new DiscordClient();
|
_bot1 = new DiscordClient();
|
||||||
_bot2 = new DiscordClient();
|
_bot2 = new DiscordClient();
|
||||||
|
|
||||||
_bot1.Connect(Settings.Test1_Username, Settings.Test1_Password).Wait();
|
_bot1.Connect(_settings.User1.Email, _settings.User1.Password).Wait();
|
||||||
_bot2.Connect(Settings.Test2_Username, Settings.Test2_Password).Wait();
|
_bot2.Connect(_settings.User2.Email, _settings.User2.Password).Wait();
|
||||||
|
|
||||||
//Cleanup existing servers
|
//Cleanup existing servers
|
||||||
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
||||||
@@ -58,10 +61,10 @@ namespace Discord.Net.Tests
|
|||||||
private void TestCreateRoom(string type)
|
private void TestCreateRoom(string type)
|
||||||
{
|
{
|
||||||
Channel channel = null;
|
Channel channel = null;
|
||||||
string name = $"test_{_random.Next()}";
|
string name = $"#test_{_random.Next()}";
|
||||||
AssertEvent<DiscordClient.ChannelEventArgs>(
|
AssertEvent<DiscordClient.ChannelEventArgs>(
|
||||||
"ChannelCreated event never received",
|
"ChannelCreated event never received",
|
||||||
() => channel = _bot1.CreateChannel(_testServer, name, type).Result,
|
() => channel = _bot1.CreateChannel(_testServer, name.Substring(1), type).Result,
|
||||||
x => _bot2.ChannelCreated += x,
|
x => _bot2.ChannelCreated += x,
|
||||||
x => _bot2.ChannelCreated -= x,
|
x => _bot2.ChannelCreated -= x,
|
||||||
(s, e) => e.Channel.Name == name);
|
(s, e) => e.Channel.Name == name);
|
||||||
@@ -74,8 +77,8 @@ namespace Discord.Net.Tests
|
|||||||
(s, e) => e.Channel.Name == name);
|
(s, e) => e.Channel.Name == name);
|
||||||
}
|
}
|
||||||
|
|
||||||
[TestCleanup]
|
[ClassCleanup]
|
||||||
public void Cleanup()
|
public static void Cleanup()
|
||||||
{
|
{
|
||||||
if (_bot1.IsConnected)
|
if (_bot1.IsConnected)
|
||||||
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
Task.WaitAll(_bot1.Servers.Select(x => _bot1.LeaveServer(x)).ToArray());
|
||||||
@@ -86,22 +89,25 @@ namespace Discord.Net.Tests
|
|||||||
_bot2.Disconnect().Wait();
|
_bot2.Disconnect().Wait();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
|
private static void AssertEvent<TArgs>(string msg, Action action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null)
|
||||||
{
|
{
|
||||||
ManualResetEvent trigger = new ManualResetEvent(false);
|
ManualResetEventSlim trigger = new ManualResetEventSlim(false);
|
||||||
bool result = false;
|
bool result = false;
|
||||||
|
|
||||||
EventHandler<TArgs> handler = (s, e) =>
|
EventHandler<TArgs> handler = (s, e) =>
|
||||||
{
|
{
|
||||||
if (test != null)
|
if (test != null)
|
||||||
|
{
|
||||||
result |= test(s, e);
|
result |= test(s, e);
|
||||||
|
trigger.Set();
|
||||||
|
}
|
||||||
else
|
else
|
||||||
result = true;
|
result = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
addEvent(handler);
|
addEvent(handler);
|
||||||
action();
|
action();
|
||||||
trigger.WaitOne(EventTimeout);
|
trigger.Wait(EventTimeout);
|
||||||
removeEvent(handler);
|
removeEvent(handler);
|
||||||
|
|
||||||
Assert.AreEqual(true, result, msg);
|
Assert.AreEqual(true, result, msg);
|
||||||
|
|||||||
10
test/Discord.Net.Tests/config.json.example
Normal file
10
test/Discord.Net.Tests/config.json.example
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"user1": {
|
||||||
|
"email": "user1@example.com",
|
||||||
|
"password": "password123"
|
||||||
|
},
|
||||||
|
"user2": {
|
||||||
|
"email": "user2@example.com",
|
||||||
|
"password": "password456"
|
||||||
|
}
|
||||||
|
}
|
||||||
4
test/Discord.Net.Tests/packages.config
Normal file
4
test/Discord.Net.Tests/packages.config
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<packages>
|
||||||
|
<package id="Newtonsoft.Json" version="7.0.1" targetFramework="net46" />
|
||||||
|
</packages>
|
||||||
Reference in New Issue
Block a user