Login Tests + Login as User with Token + Login as User with Invalid Token + Login as Bot with Token + Login as Bot with Invalid Tokenpull/62/head
@@ -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"); | |||||
} | |||||
} | |||||
} |
@@ -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; | |||||
} | |||||
} |
@@ -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 | |||||
} | |||||
} |
@@ -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'"); | |||||
} | |||||
} | |||||
} |