diff --git a/test/Discord.Net.Tests/Rest/LoginTests.cs b/test/Discord.Net.Tests/Rest/LoginTests.cs new file mode 100644 index 000000000..072588e7f --- /dev/null +++ b/test/Discord.Net.Tests/Rest/LoginTests.cs @@ -0,0 +1,56 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Rest; + +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(); + } + + [TestMethod] + [TestCategory("Login")] + public async Task Test_Login_As_User() + { + Responses.Users.UserHandlers.Mode = Rest.Responses.Users.TestMode.User; + await _client.Login(TokenType.User, "UserToken_Voltana"); + } + [TestMethod] + [ExpectedException(typeof(Net.HttpException))] + [TestCategory("Login")] + public async Task Test_Login_As_User_With_Invalid_Token() + { + Responses.Users.UserHandlers.Mode = Rest.Responses.Users.TestMode.User; + await _client.Login(TokenType.User, "UserToken-NotVoltana"); + } + [TestMethod] + [TestCategory("Login")] + public async Task Test_Login_As_Bot() + { + Responses.Users.UserHandlers.Mode = Rest.Responses.Users.TestMode.Bot; + await _client.Login(TokenType.Bot, "UserToken_VoltanaBot"); + } + [TestMethod] + [ExpectedException(typeof(Net.HttpException))] + [TestCategory("Login")] + public async Task Test_Login_As_Bot_With_Invalid_Token() + { + Responses.Users.UserHandlers.Mode = Rest.Responses.Users.TestMode.Bot; + await _client.Login(TokenType.Bot, "UserToken-NotVoltanaBot"); + } + } +} diff --git a/test/Discord.Net.Tests/Rest/Responses/Users/Me_Mocks.cs b/test/Discord.Net.Tests/Rest/Responses/Users/Me_Mocks.cs new file mode 100644 index 000000000..283a2dc1a --- /dev/null +++ b/test/Discord.Net.Tests/Rest/Responses/Users/Me_Mocks.cs @@ -0,0 +1,38 @@ +using Newtonsoft.Json; + +namespace Discord.Tests.Rest.Responses.Users +{ + public class Mock_Me_User_Valid + { + [JsonProperty("id")] + public string Id => "66078337084162048"; + [JsonProperty("username")] + public string Username => "Voltana"; + [JsonProperty("discriminator")] + public ushort Discriminator => 0001; + [JsonProperty("avatar")] + public string Avatar => "ec2b259bfe24686bf9d214b6bebe0834"; + [JsonProperty("verified")] + public bool IsVerified => true; + [JsonProperty("email")] + public string Email => "hello-i-am-not-real@foxbot.me"; + } + + public class Mock_Me_Token_Valid + { + [JsonProperty("id")] + public string Id => "66078337084162048"; + [JsonProperty("username")] + public string Username => "foxboat"; + [JsonProperty("discriminator")] + public ushort Discriminator => 0005; + [JsonProperty("avatar")] + public string Avatar => "ec2b259bfe24686bf9d214b6bebe0834"; + [JsonProperty("verified")] + public bool IsVerified => true; + [JsonProperty("email")] + public string Email => "hello-i-am-not-real@foxbot.me"; + [JsonProperty("bot")] + public bool Bot => true; + } +} diff --git a/test/Discord.Net.Tests/Rest/Responses/Users/UserHandlers.cs b/test/Discord.Net.Tests/Rest/Responses/Users/UserHandlers.cs new file mode 100644 index 000000000..b794500ed --- /dev/null +++ b/test/Discord.Net.Tests/Rest/Responses/Users/UserHandlers.cs @@ -0,0 +1,46 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using Newtonsoft.Json; +using Discord.Net; +using System.Net; +using System; + +namespace Discord.Tests.Rest.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 JsonConvert.SerializeObject(new Mock_Me_User_Valid()); + } + + 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 JsonConvert.SerializeObject(new Mock_Me_User_Valid()); + } + } + + public enum TestMode + { + User, + Bot + } +} diff --git a/test/Discord.Net.Tests/Rest/UserTests.cs b/test/Discord.Net.Tests/Rest/UserTests.cs new file mode 100644 index 000000000..3998c2789 --- /dev/null +++ b/test/Discord.Net.Tests/Rest/UserTests.cs @@ -0,0 +1,40 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Discord.Rest; + +namespace Discord.Tests.Rest +{ + [TestClass] + public class UserTests + { + public static TestContext Context; + private static DiscordClient _client; + + [ClassInitialize] + public static async Task 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(); + Responses.Users.UserHandlers.Mode = Rest.Responses.Users.TestMode.User; + await _client.Login(TokenType.User, "UserToken_Voltana"); + } + + [TestMethod] + [TestCategory("Users")] + public static async Task Test_Get_Current_User() + { + var currentUser = await _client.GetCurrentUser(); + Assert.AreEqual(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'"); + } + } +}