@@ -1,12 +1,14 @@ | |||
| |||
Microsoft Visual Studio Solution File, Format Version 12.00 | |||
# Visual Studio 14 | |||
VisualStudioVersion = 14.0.25123.0 | |||
VisualStudioVersion = 14.0.25420.1 | |||
MinimumVisualStudioVersion = 10.0.40219.1 | |||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net", "src\Discord.Net\Discord.Net.xproj", "{91E9E7BD-75C9-4E98-84AA-2C271922E5C2}" | |||
EndProject | |||
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Discord.Net.Commands", "src\Discord.Net.Commands\Discord.Net.Commands.xproj", "{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}" | |||
EndProject | |||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Discord.Net.Tests", "test\Discord.Net.Tests\Discord.Net.Tests.csproj", "{855D6B1D-847B-42DA-BE6A-23683EA89511}" | |||
EndProject | |||
Global | |||
GlobalSection(SolutionConfigurationPlatforms) = preSolution | |||
Debug|Any CPU = Debug|Any CPU | |||
@@ -21,6 +23,10 @@ Global | |||
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{078DD7E6-943D-4D09-AFC2-D2BA58B76C9C}.Release|Any CPU.Build.0 = Release|Any CPU | |||
{855D6B1D-847B-42DA-BE6A-23683EA89511}.Debug|Any CPU.ActiveCfg = Debug|Any CPU | |||
{855D6B1D-847B-42DA-BE6A-23683EA89511}.Debug|Any CPU.Build.0 = Debug|Any CPU | |||
{855D6B1D-847B-42DA-BE6A-23683EA89511}.Release|Any CPU.ActiveCfg = Release|Any CPU | |||
{855D6B1D-847B-42DA-BE6A-23683EA89511}.Release|Any CPU.Build.0 = Release|Any CPU | |||
EndGlobalSection | |||
GlobalSection(SolutionProperties) = preSolution | |||
HideSolutionNode = FALSE | |||
@@ -3,9 +3,9 @@ | |||
[](https://www.myget.org/) | |||
[](https://discord.gg/0SBTUU1wZTYLhAAW) | |||
An unofficial .Net API Wrapper for the Discord client (http://discordapp.com). | |||
[](https://www.nuget.org/packages/Discord.Net) [](https://ci.appveyor.com/project/foxbot/discord-net/) [](https://discord.gg/0SBTUU1wZTYLhAAW) | |||
Check out the [documentation](http://rtd.discord.foxbot.me/en/docs-dev/index.html) or join the [Discord API Chat](https://discord.gg/0SBTUU1wZTVjAMPx). | |||
Discord.Net is an API wrapper for [Discord](http://discordapp.com) written in C#. | |||
## Installation | |||
### Stable (NuGet) | |||
@@ -36,6 +36,9 @@ | |||
<WarningLevel>4</WarningLevel> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Reference Include="Discord.Net"> | |||
<HintPath>..\..\src\Discord.Net\bin\Debug\netstandard1.3\Discord.Net.dll</HintPath> | |||
</Reference> | |||
<Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> | |||
<HintPath>..\..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath> | |||
<Private>True</Private> | |||
@@ -55,6 +58,18 @@ | |||
</Otherwise> | |||
</Choose> | |||
<ItemGroup> | |||
<Compile Include="Rest\Framework\Serializer.cs" /> | |||
<Compile Include="Rest\GuildTests.cs" /> | |||
<Compile Include="Rest\LoginTests.cs" /> | |||
<Compile Include="Rest\Framework\EndpointHandler.cs" /> | |||
<Compile Include="Rest\Responses\Guilds\GuildHandlers.cs" /> | |||
<Compile Include="Rest\Responses\Guilds\Guild_Mocks.cs" /> | |||
<Compile Include="Rest\Responses\Roles\Role_Mocks.cs" /> | |||
<Compile Include="Rest\Responses\Guilds\Member_Mocks.cs" /> | |||
<Compile Include="Rest\Responses\Users\User_Mocks.cs" /> | |||
<Compile Include="Rest\Responses\Users\UserHandlers.cs" /> | |||
<Compile Include="Rest\Framework\TestRestClient.cs" /> | |||
<Compile Include="Rest\UserTests.cs" /> | |||
<Compile Include="Tests.cs" /> | |||
<Compile Include="Properties\AssemblyInfo.cs" /> | |||
</ItemGroup> | |||
@@ -62,9 +77,9 @@ | |||
<None Include="packages.config" /> | |||
</ItemGroup> | |||
<ItemGroup> | |||
<ProjectReference Include="..\..\src\Discord.Net\Discord.Net.Net45.csproj"> | |||
<Project>{c6a50d24-cbd3-4e76-852c-4dca60bbd608}</Project> | |||
<Name>Discord.Net.Net45</Name> | |||
<ProjectReference Include="..\..\src\Discord.Net.Commands\Discord.Net.Commands.xproj"> | |||
<Project>{078dd7e6-943d-4d09-afc2-d2ba58b76c9c}</Project> | |||
<Name>Discord.Net.Commands</Name> | |||
</ProjectReference> | |||
</ItemGroup> | |||
<Choose> | |||
@@ -0,0 +1,44 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
namespace Discord.Tests.Framework | |||
{ | |||
public class EndpointHandler | |||
{ | |||
public static EndpointHandler Instance; | |||
public delegate string RestMessageHandler(string method, string json); | |||
public Dictionary<string, RestMessageHandler> Handlers; | |||
public EndpointHandler() | |||
{ | |||
Instance = this; | |||
// Setup Endpoints | |||
Handlers = new Dictionary<string, RestMessageHandler>(); | |||
// /users Endpoints | |||
Handlers.Add("GET->users/@me", Responses.Users.UserHandlers.Me_Handler); | |||
Handlers.Add("GET->users/96642168176807936", Responses.Users.UserHandlers.Id_User_Valid); | |||
Handlers.Add("GET->users/1", Responses.Users.UserHandlers.Id_User_Invalid); | |||
Handlers.Add("GET->users/@me/guilds", Responses.Users.UserHandlers.Me_Guilds); | |||
// /guilds endpoints | |||
Handlers.Add("GET->guilds/66078535390867456", Responses.Guilds.GuildHandlers.Id_Valid); | |||
Handlers.Add("GET->guilds/66078535390867456/bans", Responses.Guilds.GuildHandlers.Bans_Valid); | |||
Handlers.Add("GET->guilds/66078535390867456/members?limit=2147483647&offset=0", Responses.Guilds.GuildHandlers.Members_Valid); | |||
Handlers.Add("GET->guilds/66078535390867456/members/1", Responses.Guilds.GuildHandlers.Member_Invalid); | |||
Handlers.Add("GET->guilds/66078535390867456/members/66078337084162048", Responses.Guilds.GuildHandlers.Member_Valid); | |||
Handlers.Add("GET->guilds/1", Responses.Guilds.GuildHandlers.Id_Invalid); | |||
} | |||
public string HandleMessage(string method, string endpoint, string json) | |||
{ | |||
var key = $"{method}->{endpoint}"; | |||
if (Handlers.ContainsKey(key)) | |||
return Handlers[key].Invoke(method, json); | |||
throw new NotImplementedException($"{method} -> {endpoint} -> {json}"); | |||
} | |||
} | |||
} |
@@ -0,0 +1,41 @@ | |||
using Discord.Net.Converters; | |||
using Newtonsoft.Json; | |||
using System; | |||
using System.IO; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using System.Globalization; | |||
namespace Discord.Tests.Framework | |||
{ | |||
public class Json | |||
{ | |||
public static JsonSerializer Serializer; | |||
public Json() | |||
{ | |||
Serializer = new JsonSerializer(); | |||
var _serializer = new JsonSerializer(); | |||
_serializer.Converters.Add(new ChannelTypeConverter()); | |||
_serializer.Converters.Add(new ImageConverter()); | |||
_serializer.Converters.Add(new NullableUInt64Converter()); | |||
_serializer.Converters.Add(new PermissionTargetConverter()); | |||
_serializer.Converters.Add(new StringEntityConverter()); | |||
_serializer.Converters.Add(new UInt64Converter()); | |||
_serializer.Converters.Add(new UInt64EntityConverter()); | |||
_serializer.Converters.Add(new UserStatusConverter()); | |||
Serializer = _serializer; | |||
} | |||
public static string SerializeObject(object o) | |||
{ | |||
var sb = new StringBuilder(256); | |||
using (TextWriter text = new StringWriter(sb, CultureInfo.InvariantCulture)) | |||
using (JsonWriter writer = new JsonTextWriter(text)) | |||
Serializer.Serialize(writer, o); | |||
return sb.ToString(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,53 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using Discord; | |||
using Discord.Net.Rest; | |||
using System.Threading; | |||
using System.IO; | |||
namespace Discord.Tests.Framework | |||
{ | |||
class TestRestClient : IRestClient | |||
{ | |||
public static Dictionary<string, string> Headers = new Dictionary<string, string>(); | |||
public TestRestClient(string baseUrl) | |||
{ | |||
} | |||
Task<Stream> IRestClient.SendAsync(string method, string endpoint, bool headerOnly = false) | |||
{ | |||
if (headerOnly) return null; | |||
return Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes(EndpointHandler.Instance.HandleMessage(method, endpoint, "")))); | |||
} | |||
Task<Stream> IRestClient.SendAsync(string method, string endpoint, IReadOnlyDictionary<string, object> multipartParams, bool headerOnly = false) | |||
{ | |||
if (headerOnly) return null; | |||
throw new NotImplementedException("method only used for SendFile, not concerned with that yet."); | |||
} | |||
Task<Stream> IRestClient.SendAsync(string method, string endpoint, string json, bool headerOnly = false) | |||
{ | |||
if (headerOnly) return null; | |||
return Task.FromResult<Stream>(new MemoryStream(Encoding.UTF8.GetBytes(EndpointHandler.Instance.HandleMessage(method, endpoint, json)))); | |||
} | |||
void IRestClient.SetCancelToken(CancellationToken cancelToken) | |||
{ | |||
} | |||
void IRestClient.SetHeader(string key, string value) | |||
{ | |||
if (Headers.ContainsKey(key)) | |||
{ | |||
Headers.Remove(key); | |||
} | |||
Headers.Add(key, value); | |||
Console.WriteLine($"[Header Set]: {key} -> {value}"); | |||
} | |||
} | |||
} |
@@ -0,0 +1,50 @@ | |||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||
using Newtonsoft.Json; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Net; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace Discord.Tests.Framework.Responses.Guilds | |||
{ | |||
public static class GuildHandlers | |||
{ | |||
public static string Id_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/guilds/:id' is GET."); | |||
return Json.SerializeObject(Guild_Mocks.Guild_From_Id); | |||
} | |||
public static string Id_Invalid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/guilds/:id' is GET."); | |||
throw new Net.HttpException((HttpStatusCode)404); | |||
} | |||
public static string Bans_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/guilds/:id/bans' is GET."); | |||
return Json.SerializeObject(Guild_Mocks.GuildBansList); | |||
} | |||
public static string Members_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/guilds/:id/members' is GET."); | |||
return Json.SerializeObject(Member_Mocks.Guild_Members); | |||
} | |||
public static string Member_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/guilds/:id/members/:member_id' is GET."); | |||
return Json.SerializeObject(Member_Mocks.Guild_Member_1); | |||
} | |||
public static string Member_Invalid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/guilds/:id/members/:member_id' is GET."); | |||
throw new Net.HttpException((HttpStatusCode)404); | |||
} | |||
} | |||
} |
@@ -0,0 +1,70 @@ | |||
using Discord.API; | |||
using Newtonsoft.Json; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
namespace Discord.Tests.Framework.Responses.Guilds | |||
{ | |||
public static class Guild_Mocks | |||
{ | |||
public static Guild Guild_From_Id | |||
{ | |||
get | |||
{ | |||
return new Guild | |||
{ | |||
Id = 66078535390867456, | |||
Name = "Discord API", | |||
Icon = "c8829ee5a88cbe065519befa9093f63a", | |||
Splash = null, | |||
OwnerId = 66078337084162048, | |||
Region = "us-east", | |||
AFKChannelId = 66081976485941248, | |||
AFKTimeout = 3600, | |||
EmbedEnabled = true, | |||
EmbedChannelId = null, | |||
VerificationLevel = 0, | |||
Roles = new Role[] { Roles.Constant_Role_Mocks.Mock_Everyone_Role }, | |||
}; | |||
} | |||
} | |||
public static IEnumerable<User> GuildBansList => new List<User> { Users.User_Mocks.Bot_User, Users.User_Mocks.Me_User }; | |||
public static IEnumerable<UserGuild> UserGuildsList() => new List<UserGuild> { User_Guild_1, User_Guild_2 }; | |||
public static UserGuild User_Guild_1 | |||
{ | |||
get | |||
{ | |||
return new UserGuild | |||
{ | |||
Id = 41771983423143937, | |||
Name = "Discord Developers", | |||
Icon = "ae140f33228348df347067059474bb11", | |||
Owner = false, | |||
Permissions = 103926785 | |||
}; | |||
} | |||
} | |||
public static UserGuild User_Guild_2 | |||
{ | |||
get | |||
{ | |||
return new UserGuild | |||
{ | |||
Id = 81384788765712384, | |||
Name = "Discord API", | |||
Icon = "2aab26934e72b4ec300c5aa6cf67c7b3", | |||
Owner = false, | |||
Permissions = 103926785 | |||
}; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,43 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using Discord.API; | |||
namespace Discord.Tests.Framework.Responses.Guilds | |||
{ | |||
public static class Member_Mocks | |||
{ | |||
public static IEnumerable<GuildMember> Guild_Members => new List<GuildMember> { Guild_Member_1, Guild_Member_2 }; | |||
public static GuildMember Guild_Member_1 | |||
{ | |||
get | |||
{ | |||
return new GuildMember() | |||
{ | |||
User = Users.User_Mocks.Me_User, | |||
Nick = "voltana", | |||
JoinedAt = new DateTime(2009, 4, 19), | |||
Deaf = true, | |||
Mute = false, | |||
Roles = new ulong[] { 1UL } | |||
}; | |||
} | |||
} | |||
public static GuildMember Guild_Member_2 | |||
{ | |||
get | |||
{ | |||
return new GuildMember() | |||
{ | |||
User = Users.User_Mocks.Bot_User, | |||
Nick = "foxbot", | |||
JoinedAt = new DateTime(2016, 5, 5), | |||
}; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,30 @@ | |||
using Newtonsoft.Json; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using Discord.API; | |||
namespace Discord.Tests.Framework.Responses.Roles | |||
{ | |||
public static class Constant_Role_Mocks | |||
{ | |||
public static Role Mock_Everyone_Role | |||
{ | |||
get | |||
{ | |||
return new Role | |||
{ | |||
Id = 66078535390867456, | |||
Color = 0, | |||
Position = 0, | |||
Hoist = false, | |||
Managed = false, | |||
Name = "@everyone", | |||
Permissions = 36953089 | |||
}; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,64 @@ | |||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||
using Newtonsoft.Json; | |||
using Discord.Net; | |||
using System.Net; | |||
using System; | |||
namespace Discord.Tests.Framework.Responses.Users | |||
{ | |||
public static class UserHandlers | |||
{ | |||
public static TestMode Mode; | |||
public static string Me_Handler(string method, string json) | |||
{ | |||
switch (Mode) | |||
{ | |||
case TestMode.User: | |||
return Me_User_Valid(method, json); | |||
case TestMode.Bot: | |||
return Me_Bot_Valid(method, json); | |||
default: | |||
throw new ArgumentException("TestMode was set incorrectly."); | |||
} | |||
} | |||
public static string Me_User_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/users/@me' is GET."); | |||
if (TestRestClient.Headers["authorization"] != "UserToken_Voltana") throw new HttpException((HttpStatusCode)401); | |||
return Json.SerializeObject(User_Mocks.Me_User); | |||
} | |||
public static string Me_Bot_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/users/@me' is GET."); | |||
if (TestRestClient.Headers["authorization"] != "Bot UserToken_VoltanaBot") throw new HttpException((HttpStatusCode)401); | |||
return Json.SerializeObject(User_Mocks.Bot_User); | |||
} | |||
public static string Id_User_Valid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/users/:id' is GET"); | |||
return Json.SerializeObject(User_Mocks.Public_User); | |||
} | |||
public static string Id_User_Invalid(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/users/:id' is GET"); | |||
throw new HttpException((HttpStatusCode)404); | |||
} | |||
public static string Me_Guilds(string method, string json) | |||
{ | |||
Assert.AreEqual("GET", method, "Expected method to '/users/@me/guilds' is GET"); | |||
return Json.SerializeObject(Guilds.Guild_Mocks.UserGuildsList()); | |||
} | |||
} | |||
public enum TestMode | |||
{ | |||
User, | |||
Bot | |||
} | |||
} |
@@ -0,0 +1,55 @@ | |||
using Newtonsoft.Json; | |||
using Discord.API; | |||
namespace Discord.Tests.Framework.Responses.Users | |||
{ | |||
public static class User_Mocks | |||
{ | |||
public static User Me_User | |||
{ | |||
get | |||
{ | |||
return new User | |||
{ | |||
Id = 66078337084162048, | |||
Username = "Voltana", | |||
Discriminator = "0001", | |||
Avatar = "ec2b259bfe24686bf9d214b6bebe0834", | |||
Verified = true, | |||
Email = "hello-i-am-not-real@foxbot.me" | |||
}; | |||
} | |||
} | |||
public static User Bot_User | |||
{ | |||
get | |||
{ | |||
return new User | |||
{ | |||
Id = 66078337084162048, | |||
Username = "foxboat", | |||
Discriminator = "0005", | |||
Avatar = "ec2b259bfe24686bf9d214b6bebe0834", | |||
Verified = true, | |||
Email = "hello-i-am-not-real@foxbot.me", | |||
Bot = true | |||
}; | |||
} | |||
} | |||
public static User Public_User | |||
{ | |||
get | |||
{ | |||
return new User | |||
{ | |||
Id = 96642168176807936, | |||
Username = "Khionu", | |||
Discriminator = "9999", | |||
Avatar = "ceeff590f1e0e1ccae0afc89967131ff" | |||
}; | |||
} | |||
} | |||
} | |||
} |
@@ -0,0 +1,120 @@ | |||
using Discord; | |||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using Discord.Tests.Framework; | |||
namespace Discord.Tests.Rest | |||
{ | |||
[TestClass] | |||
public class GuildTests | |||
{ | |||
public static TestContext Context; | |||
private static DiscordClient _client; | |||
[ClassInitialize] | |||
public static void Initialize(TestContext context) | |||
{ | |||
Context = context; | |||
_client = new DiscordClient(new DiscordConfig() { RestClientProvider = (url, ct) => new TestRestClient(url, ct) }); | |||
if (EndpointHandler.Instance == null) EndpointHandler.Instance = new EndpointHandler(); | |||
if (Json.Serializer == null) new Json(); | |||
Framework.Responses.Users.UserHandlers.Mode = Framework.Responses.Users.TestMode.User; | |||
_client.LoginAsync(TokenType.User, "UserToken_Voltana").GetAwaiter().GetResult(); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Get_Guild() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
Assert.AreEqual(66078535390867456UL, guild.Id, "Expected ID '66078535390867456'"); | |||
Assert.AreEqual("Discord API", guild.Name, "Expected Name 'Discord API'"); | |||
// Cannot Verify Guild URL, ID not publicly exposed. | |||
Assert.IsNull(guild.SplashUrl, "Expected SplashUrl 'null'"); | |||
Assert.AreEqual(66078337084162048UL, guild.OwnerId, "Expected OwnerId '66078337084162048'"); | |||
Assert.AreEqual(3600, guild.AFKTimeout, "Expected AFKTimeout '3600'"); | |||
Assert.AreEqual(true, guild.IsEmbeddable, "Expected Embeddable 'true'"); | |||
Assert.IsNull(guild.EmbedChannelId, "Expected EmbedChannelId 'null'"); | |||
Assert.AreEqual(0, guild.VerificationLevel, "Expected VerificationLevel '0'"); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Get_Guild_Invalid_Id() | |||
{ | |||
var guild = await _client.GetGuildAsync(1); | |||
Assert.IsNull(guild); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Get_Guilds() | |||
{ | |||
var guilds = await _client.GetGuildsAsync(); | |||
Assert.AreEqual(2, guilds.Count(), "Expected 2 Guilds"); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Bans() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var bans = await guild.GetBansAsync(); | |||
Assert.AreEqual(2, bans.Count()); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_User() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var user = await guild.GetUserAsync(66078337084162048); | |||
// TODO: Asserts | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Invalid_User() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var user = await guild.GetUserAsync(1); | |||
Assert.IsNull(user, "Expected returned user to be null"); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Users() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var users = await guild.GetUsersAsync(); | |||
Assert.AreEqual(2, users.Count()); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Role() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var role = guild.GetRole(1); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Invalid_Role() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var role = guild.GetRole(1); | |||
Assert.IsNull(role, "Expected returned role to be null."); | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Roles() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var roles = guild.Roles; | |||
} | |||
[TestMethod] | |||
[TestCategory("Guilds")] | |||
public async Task Test_Guild_Get_Invites() | |||
{ | |||
var guild = await _client.GetGuildAsync(66078535390867456); | |||
var invites = await guild.GetInvitesAsync(); | |||
} | |||
} | |||
} |
@@ -0,0 +1,59 @@ | |||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using Discord; | |||
using Discord.Tests.Framework; | |||
using Discord.Tests.Framework.Responses; | |||
namespace Discord.Tests.Rest | |||
{ | |||
[TestClass] | |||
public class LoginTests | |||
{ | |||
public static TestContext Context; | |||
private static DiscordClient _client; | |||
[ClassInitialize] | |||
public static void Initialize(TestContext context) | |||
{ | |||
Context = context; | |||
_client = new DiscordClient(new DiscordConfig() { RestClientProvider = (url, ct) => new TestRestClient(url, ct) }); | |||
if (EndpointHandler.Instance == null) EndpointHandler.Instance = new EndpointHandler(); | |||
if (Json.Serializer == null) new Json(); | |||
} | |||
[TestMethod] | |||
[TestCategory("Login")] | |||
public async Task Test_Login_As_User() | |||
{ | |||
Framework.Responses.Users.UserHandlers.Mode = Framework.Responses.Users.TestMode.User; | |||
await _client.LoginAsync(TokenType.User, "UserToken_Voltana"); | |||
} | |||
[TestMethod] | |||
[ExpectedException(typeof(Net.HttpException))] | |||
[TestCategory("Login")] | |||
public async Task Test_Login_As_User_With_Invalid_Token() | |||
{ | |||
Framework.Responses.Users.UserHandlers.Mode = Framework.Responses.Users.TestMode.User; | |||
await _client.LoginAsync(TokenType.User, "UserToken-NotVoltana"); | |||
} | |||
[TestMethod] | |||
[TestCategory("Login")] | |||
public async Task Test_Login_As_Bot() | |||
{ | |||
Framework.Responses.Users.UserHandlers.Mode = Framework.Responses.Users.TestMode.Bot; | |||
await _client.LoginAsync(TokenType.Bot, "UserToken_VoltanaBot"); | |||
} | |||
[TestMethod] | |||
[ExpectedException(typeof(Net.HttpException))] | |||
[TestCategory("Login")] | |||
public async Task Test_Login_As_Bot_With_Invalid_Token() | |||
{ | |||
Framework.Responses.Users.UserHandlers.Mode = Framework.Responses.Users.TestMode.Bot; | |||
await _client.LoginAsync(TokenType.Bot, "UserToken-NotVoltanaBot"); | |||
} | |||
} | |||
} |
@@ -0,0 +1,65 @@ | |||
using Microsoft.VisualStudio.TestTools.UnitTesting; | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Text; | |||
using System.Threading.Tasks; | |||
using Discord; | |||
using Discord.Tests.Framework; | |||
namespace Discord.Tests.Rest | |||
{ | |||
[TestClass] | |||
public class UserTests | |||
{ | |||
public static TestContext Context; | |||
private static DiscordClient _client; | |||
[ClassInitialize] | |||
public static void Initialize(TestContext context) | |||
{ | |||
Context = context; | |||
_client = new DiscordClient(new DiscordConfig() { RestClientProvider = (url, ct) => new TestRestClient(url, ct) }); | |||
if (EndpointHandler.Instance == null) EndpointHandler.Instance = new EndpointHandler(); | |||
if (Json.Serializer == null) new Json(); | |||
Framework.Responses.Users.UserHandlers.Mode = Framework.Responses.Users.TestMode.User; | |||
_client.LoginAsync(TokenType.User, "UserToken_Voltana").GetAwaiter().GetResult(); | |||
} | |||
[TestMethod] | |||
[TestCategory("Users")] | |||
public async Task Test_Get_Current_User() | |||
{ | |||
var currentUser = await _client.GetCurrentUserAsync(); | |||
Assert.AreEqual((UInt64)66078337084162048, currentUser.Id, "Expected Id '66078337084162048'"); | |||
Assert.AreEqual("Voltana", currentUser.Username, "Expected Name 'Voltana'"); | |||
Assert.AreEqual(0001, currentUser.Discriminator, "Expected Discriminator '0001'"); | |||
// Cannot Test Avatar URLs, Avatar ID not exposed publicly. | |||
Assert.AreEqual(true, currentUser.IsVerified, "Expected Verified 'true'"); | |||
Assert.AreEqual("hello-i-am-not-real@foxbot.me", currentUser.Email, "Expected Email 'hello-i-am-not-real@foxbot.me'"); | |||
Assert.AreEqual(false, currentUser.IsBot, "Expected IsBot 'false'"); | |||
Assert.AreEqual("<@66078337084162048>", currentUser.Mention, "Expected Mention '<@66078337084162048>'"); | |||
Assert.AreEqual(new DateTime(635714215032370000), currentUser.CreatedAt, "Expected Created At '635714215032370000'"); | |||
} | |||
[TestMethod] | |||
[TestCategory("Users")] | |||
public async Task Test_Get_User() | |||
{ | |||
var user = await _client.GetUserAsync(96642168176807936); | |||
Assert.AreEqual((UInt64)96642168176807936, user.Id, "Expected Id '96642168176807936'"); | |||
Assert.AreEqual("Khionu", user.Username, "Expected Name 'Khionu'"); | |||
Assert.AreEqual(9999, user.Discriminator, "Expected Discriminator '0001'"); | |||
// Cannot Test Avatar URLs, Avatar ID not exposed publicly. | |||
Assert.AreEqual(false, user.IsBot, "Expected IsBot 'false'"); | |||
Assert.AreEqual("<@!96642168176807936>", user.NicknameMention, "Expected Mention '<@!96642168176807936>'"); | |||
Assert.AreEqual(new DateTime(635787084884180000), user.CreatedAt, "Expected Created At '635787084884180000'"); | |||
} | |||
[TestMethod] | |||
[TestCategory("Users")] | |||
public async Task Test_Get_Invalid_User() | |||
{ | |||
var user = await _client.GetUserAsync(1); | |||
Assert.IsNull(user, "Expected Invalid User to be 'null'"); | |||
} | |||
} | |||
} |
@@ -7,8 +7,11 @@ using System.Threading.Tasks; | |||
namespace Discord.Tests | |||
{ | |||
//TODO: Tests are massively incomplete and out of date, needing a full rewrite | |||
// these tests are really bad | |||
// we're never going to look at them again | |||
// but in case i do decide to look at them again they are still here | |||
/* | |||
[TestClass] | |||
public class Tests | |||
{ | |||
@@ -311,184 +314,186 @@ namespace Discord.Tests | |||
async () => await _targetBot.CurrentUser.Modify(TargetPassword, name), | |||
x => _obGuildBot.UserUpdated += x, | |||
x => _obGuildBot.UserUpdated -= x, | |||
(s, e) => e.After.Username == name);*/ | |||
} | |||
[TestMethod] | |||
public void TestSetStatus() | |||
{ | |||
AssertEvent<UserUpdatedEventArgs>( | |||
"UserUpdated never fired", | |||
async () => await SetStatus(_targetBot, UserStatus.Idle), | |||
x => _observerBot.UserUpdated += x, | |||
x => _observerBot.UserUpdated -= x, | |||
(s, e) => e.After.Status == UserStatus.Idle); | |||
} | |||
private Task SetStatus(DiscordClient _client, UserStatus status) | |||
{ | |||
throw new NotImplementedException(); | |||
/*_client.SetStatus(status); | |||
await Task.Delay(50);*/ | |||
} | |||
[TestMethod] | |||
public void TestSetGame() | |||
{ | |||
AssertEvent<UserUpdatedEventArgs>( | |||
"UserUpdated never fired", | |||
async () => await SetGame(_targetBot, "test game"), | |||
x => _observerBot.UserUpdated += x, | |||
x => _observerBot.UserUpdated -= x, | |||
(s, e) => _targetBot.CurrentUser.CurrentGame == "test game"); | |||
} | |||
private Task SetGame(DiscordClient _client, string game) | |||
{ | |||
throw new NotImplementedException(); | |||
//_client.SetGame(game); | |||
//await Task.Delay(5); | |||
} | |||
(s, e) => e.After.Username == name); | |||
}*/ | |||
/* | |||
[TestMethod] | |||
public void TestSetStatus() | |||
{ | |||
AssertEvent<UserUpdatedEventArgs>( | |||
"UserUpdated never fired", | |||
async () => await SetStatus(_targetBot, UserStatus.Idle), | |||
x => _observerBot.UserUpdated += x, | |||
x => _observerBot.UserUpdated -= x, | |||
(s, e) => e.After.Status == UserStatus.Idle); | |||
} | |||
private Task SetStatus(DiscordClient _client, UserStatus status) | |||
{ | |||
throw new NotImplementedException(); | |||
/*_client.SetStatus(status); | |||
await Task.Delay(50); | |||
}*/ | |||
/* | |||
[TestMethod] | |||
public void TestSetGame() | |||
{ | |||
AssertEvent<UserUpdatedEventArgs>( | |||
"UserUpdated never fired", | |||
async () => await SetGame(_targetBot, "test game"), | |||
x => _observerBot.UserUpdated += x, | |||
x => _observerBot.UserUpdated -= x, | |||
(s, e) => _targetBot.CurrentUser.CurrentGame == "test game"); | |||
#endregion | |||
} | |||
private Task SetGame(DiscordClient _client, string game) | |||
{ | |||
throw new NotImplementedException(); | |||
//_client.SetGame(game); | |||
//await Task.Delay(5); | |||
} | |||
#region Permission Tests | |||
#endregion | |||
// Permissions | |||
[TestMethod] | |||
public async Task Test_AddGet_PermissionsRule() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
await channel.UpdatePermissionOverwrite(user, perms); | |||
var resultPerms = channel.GetPermissionOverwrite(user); | |||
Assert.IsNotNull(resultPerms, "Perms retrieved from Guild were null."); | |||
} | |||
[TestMethod] | |||
public async Task Test_AddRemove_PermissionsRule() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
await channel.UpdatePermissionOverwrite(user, perms); | |||
await channel.RemovePermissionOverwrite(user); | |||
await Task.Delay(200); | |||
Assert.AreEqual(PermValue.Inherit, channel.GetPermissionOverwrite(user)?.SendMessages); | |||
} | |||
[TestMethod] | |||
public async Task Test_Permissions_Event() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
AssertEvent<ChannelUpdatedEventArgs> | |||
("ChannelUpdatedEvent never fired.", | |||
async () => await channel.UpdatePermissionOverwrite(user, perms), | |||
x => _targetBot.ChannelUpdated += x, | |||
x => _targetBot.ChannelUpdated -= x, | |||
(s, e) => e.Channel == channel && (e.After as GuildChannel).PermissionOverwrites.Count() != (e.Before as GuildChannel).PermissionOverwrites.Count()); | |||
} | |||
[TestMethod] | |||
[ExpectedException(typeof(Net.HttpException))] | |||
public async Task Test_Affect_Permissions_Invalid_Channel() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
await channel.Delete(); | |||
await channel.UpdatePermissionOverwrite(user, perms); | |||
} | |||
#region Permission Tests | |||
#endregion | |||
// Permissions | |||
[TestMethod] | |||
public async Task Test_AddGet_PermissionsRule() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
await channel.UpdatePermissionOverwrite(user, perms); | |||
var resultPerms = channel.GetPermissionOverwrite(user); | |||
Assert.IsNotNull(resultPerms, "Perms retrieved from Guild were null."); | |||
} | |||
[TestMethod] | |||
public async Task Test_AddRemove_PermissionsRule() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
await channel.UpdatePermissionOverwrite(user, perms); | |||
await channel.RemovePermissionOverwrite(user); | |||
await Task.Delay(200); | |||
Assert.AreEqual(PermValue.Inherit, channel.GetPermissionOverwrite(user)?.SendMessages); | |||
} | |||
[TestMethod] | |||
public async Task Test_Permissions_Event() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
AssertEvent<ChannelUpdatedEventArgs> | |||
("ChannelUpdatedEvent never fired.", | |||
async () => await channel.UpdatePermissionOverwrite(user, perms), | |||
x => _targetBot.ChannelUpdated += x, | |||
x => _targetBot.ChannelUpdated -= x, | |||
(s, e) => e.Channel == channel && (e.After as GuildChannel).PermissionOverwrites.Count() != (e.Before as GuildChannel).PermissionOverwrites.Count()); | |||
} | |||
[TestMethod] | |||
[ExpectedException(typeof(Net.HttpException))] | |||
public async Task Test_Affect_Permissions_Invalid_Channel() | |||
{ | |||
var channel = await _testGuild.CreateTextChannel(GetRandomText()); | |||
var user = _testGuild.GetUser(_targetBot.CurrentUser.Id); | |||
var perms = new OverwritePermissions(sendMessages: PermValue.Deny); | |||
await channel.Delete(); | |||
await channel.UpdatePermissionOverwrite(user, perms); | |||
} | |||
#endregion | |||
[ClassCleanup] | |||
public static async Task Cleanup() | |||
{ | |||
WaitMany( | |||
(await _hostBot.GetGuilds()).Select(x => x.Owner.Id == _hostBot.CurrentUser.Id ? x.Delete() : x.Leave()), | |||
(await _targetBot.GetGuilds()).Select(x => x.Owner.Id == _targetBot.CurrentUser.Id ? x.Delete() : x.Leave()), | |||
(await _observerBot.GetGuilds()).Select(x => x.Owner.Id == _observerBot.CurrentUser.Id ? x.Delete() : x.Leave())); | |||
WaitAll( | |||
_hostBot.Disconnect(), | |||
_targetBot.Disconnect(), | |||
_observerBot.Disconnect()); | |||
} | |||
#region Helpers | |||
[ClassCleanup] | |||
public static async Task Cleanup() | |||
{ | |||
WaitMany( | |||
(await _hostBot.GetGuilds()).Select(x => x.Owner.Id == _hostBot.CurrentUser.Id ? x.Delete() : x.Leave()), | |||
(await _targetBot.GetGuilds()).Select(x => x.Owner.Id == _targetBot.CurrentUser.Id ? x.Delete() : x.Leave()), | |||
(await _observerBot.GetGuilds()).Select(x => x.Owner.Id == _observerBot.CurrentUser.Id ? x.Delete() : x.Leave())); | |||
WaitAll( | |||
_hostBot.Disconnect(), | |||
_targetBot.Disconnect(), | |||
_observerBot.Disconnect()); | |||
} | |||
// Task Helpers | |||
#region Helpers | |||
private static void AssertEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null) | |||
{ | |||
AssertEvent(msg, action, addEvent, removeEvent, test, true); | |||
} | |||
private static void AssertNoEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null) | |||
{ | |||
AssertEvent(msg, action, addEvent, removeEvent, test, false); | |||
} | |||
private static void AssertEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test, bool assertTrue) | |||
{ | |||
ManualResetEventSlim trigger = new ManualResetEventSlim(false); | |||
bool result = false; | |||
// Task Helpers | |||
EventHandler<TArgs> handler = (s, e) => | |||
{ | |||
if (test != null) | |||
{ | |||
result |= test(s, e); | |||
trigger.Set(); | |||
} | |||
else | |||
result = true; | |||
}; | |||
addEvent(handler); | |||
var task = action(); | |||
trigger.Wait(EventTimeout); | |||
task.Wait(); | |||
removeEvent(handler); | |||
Assert.AreEqual(assertTrue, result, msg); | |||
} | |||
private static void AssertEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null) | |||
{ | |||
AssertEvent(msg, action, addEvent, removeEvent, test, true); | |||
} | |||
private static void AssertNoEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test = null) | |||
{ | |||
AssertEvent(msg, action, addEvent, removeEvent, test, false); | |||
} | |||
private static void AssertEvent<TArgs>(string msg, Func<Task> action, Action<EventHandler<TArgs>> addEvent, Action<EventHandler<TArgs>> removeEvent, Func<object, TArgs, bool> test, bool assertTrue) | |||
{ | |||
ManualResetEventSlim trigger = new ManualResetEventSlim(false); | |||
bool result = false; | |||
private static void AssertEvent(string msg, Func<Task> action, Action<EventHandler> addEvent, Action<EventHandler> removeEvent, Func<object, bool> test, bool assertTrue) | |||
EventHandler<TArgs> handler = (s, e) => | |||
{ | |||
ManualResetEventSlim trigger = new ManualResetEventSlim(false); | |||
bool result = false; | |||
EventHandler handler = (s, e) => | |||
if (test != null) | |||
{ | |||
if (test != null) | |||
{ | |||
result |= test(s); | |||
trigger.Set(); | |||
} | |||
else | |||
result = true; | |||
}; | |||
addEvent(handler); | |||
var task = action(); | |||
trigger.Wait(EventTimeout); | |||
task.Wait(); | |||
removeEvent(handler); | |||
Assert.AreEqual(assertTrue, result, msg); | |||
} | |||
result |= test(s, e); | |||
trigger.Set(); | |||
} | |||
else | |||
result = true; | |||
}; | |||
addEvent(handler); | |||
var task = action(); | |||
trigger.Wait(EventTimeout); | |||
task.Wait(); | |||
removeEvent(handler); | |||
Assert.AreEqual(assertTrue, result, msg); | |||
} | |||
private static void WaitAll(params Task[] tasks) | |||
{ | |||
Task.WaitAll(tasks); | |||
} | |||
private static void WaitAll(IEnumerable<Task> tasks) | |||
{ | |||
Task.WaitAll(tasks.ToArray()); | |||
} | |||
private static void WaitMany(params IEnumerable<Task>[] tasks) | |||
private static void AssertEvent(string msg, Func<Task> action, Action<EventHandler> addEvent, Action<EventHandler> removeEvent, Func<object, bool> test, bool assertTrue) | |||
{ | |||
ManualResetEventSlim trigger = new ManualResetEventSlim(false); | |||
bool result = false; | |||
EventHandler handler = (s, e) => | |||
{ | |||
Task.WaitAll(tasks.Where(x => x != null).SelectMany(x => x).ToArray()); | |||
} | |||
if (test != null) | |||
{ | |||
result |= test(s); | |||
trigger.Set(); | |||
} | |||
else | |||
result = true; | |||
}; | |||
addEvent(handler); | |||
var task = action(); | |||
trigger.Wait(EventTimeout); | |||
task.Wait(); | |||
removeEvent(handler); | |||
Assert.AreEqual(assertTrue, result, msg); | |||
} | |||
#endregion | |||
private static void WaitAll(params Task[] tasks) | |||
{ | |||
Task.WaitAll(tasks); | |||
} | |||
private static void WaitAll(IEnumerable<Task> tasks) | |||
{ | |||
Task.WaitAll(tasks.ToArray()); | |||
} | |||
private static void WaitMany(params IEnumerable<Task>[] tasks) | |||
{ | |||
Task.WaitAll(tasks.Where(x => x != null).SelectMany(x => x).ToArray()); | |||
} | |||
#endregion | |||
}*/ | |||
} |