@@ -7,7 +7,7 @@ | |||
<Import Project="$(VSToolsPath)\DotNet\Microsoft.DotNet.Props" Condition="'$(VSToolsPath)' != ''" /> | |||
<PropertyGroup Label="Globals"> | |||
<ProjectGuid>69eecb8d-8705-424f-9202-f7f4051ee403</ProjectGuid> | |||
<RootNamespace>Discord.Net.Tests</RootNamespace> | |||
<RootNamespace>Discord.Tests</RootNamespace> | |||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">.\obj</BaseIntermediateOutputPath> | |||
<OutputPath Condition="'$(OutputPath)'=='' ">.\bin\</OutputPath> | |||
</PropertyGroup> | |||
@@ -0,0 +1,23 @@ | |||
using Discord.API; | |||
namespace Discord.Tests.Framework.Mocks.Rest | |||
{ | |||
public static class Guilds | |||
{ | |||
public static Guild DiscordApi => new Guild() | |||
{ | |||
Id = 81384788765712384, | |||
Name = "Discord API", | |||
OwnerId = 53905483156684800, | |||
MfaLevel = 0, | |||
VerificationLevel = 0, | |||
Roles = new Role[] { Roles.Everyone(81384788765712384), Roles.DiscordEmployee, Roles.LibraryDevs }, | |||
AFKTimeout = 3600, | |||
Region = "us-east", | |||
DefaultMessageNotifications = (DefaultMessageNotifications)1, | |||
EmbedChannelId = 81384788765712384, | |||
EmbedEnabled = true, | |||
Icon = "2aab26934e72b4ec300c5aa6cf67c7b3" | |||
}; | |||
} | |||
} |
@@ -0,0 +1,43 @@ | |||
using Discord.API; | |||
namespace Discord.Tests.Framework.Mocks.Rest | |||
{ | |||
public static class Roles | |||
{ | |||
// TODO: These mocks need to include 'mentionable' when the library implements it. | |||
public static Role Everyone(ulong guildId) => new Role() | |||
{ | |||
Id = guildId, | |||
Name = "@everyone", | |||
Color = 0, | |||
Hoist = false, | |||
Permissions = 104324097, | |||
Position = 0, | |||
Managed = false | |||
}; | |||
public static Role LibraryDevs => new Role() | |||
{ | |||
Id = 81793792671232000, | |||
Name = "Library Devs", | |||
Color = 42607, | |||
Hoist = true, | |||
Permissions = 268435456, | |||
Position = 17, | |||
Managed = false | |||
}; | |||
public static Role DiscordEmployee => new Role() | |||
{ | |||
Id = 103548914652696576, | |||
Name = "Discord Employee", | |||
Color = 10181046, | |||
Hoist = false, | |||
Permissions = 29368358, | |||
Position = 20, | |||
Managed = false | |||
}; | |||
} | |||
} |
@@ -3,10 +3,14 @@ using System.Collections.Generic; | |||
using System.IO; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
using System.Net; | |||
using System.Text; | |||
using UserRoutes = Discord.Tests.Framework.Routes.Users; | |||
using GuildRoutes = Discord.Tests.Framework.Routes.Guilds; | |||
using Contracts = Discord.Tests.Framework.Routes.Contracts; | |||
using Newtonsoft.Json; | |||
using Discord.Net.Converters; | |||
using Discord.Net; | |||
namespace Discord.Tests.Framework | |||
{ | |||
@@ -18,16 +22,23 @@ namespace Discord.Tests.Framework | |||
internal Dictionary<string, Response> Routes = new Dictionary<string, Response>() | |||
{ | |||
// --- USERS | |||
// Get Current User | |||
["GET users/@me"] = new Response(UserRoutes.Me), | |||
// Get User by ID | |||
["GET users/66078337084162048"] = new Response(UserRoutes.Public), | |||
["GET users/1"] = new Response(UserRoutes.InvalidPublic) | |||
// Get User by Tag | |||
["GET users?q=foxbot%230282&limit=1"] = new Response(UserRoutes.Query), | |||
// --- GUILDS | |||
["GET guilds/81384788765712384"] = new Response(GuildRoutes.DiscordApi) | |||
}; | |||
internal Stream GetMock(string method, string endpoint, string json, IReadOnlyDictionary<string, string> requestHeaders) | |||
{ | |||
var key = string.Format("{0} {1}", method.ToUpperInvariant(), endpoint.ToLowerInvariant()); | |||
if (!Routes.ContainsKey(key)) | |||
throw new NotImplementedException($"{key}: {json}"); | |||
throw new HttpException(HttpStatusCode.NotFound, $"{key}: {json}"); | |||
Contracts.EnsureAuthorization(requestHeaders); | |||
var model = Routes[key].Invoke(json, requestHeaders); | |||
var textResponse = JsonConvert.SerializeObject(model, SerializerSettings); | |||
return new MemoryStream(Encoding.UTF8.GetBytes(textResponse)); | |||
@@ -0,0 +1,11 @@ | |||
using System.Collections.Generic; | |||
using GuildMocks = Discord.Tests.Framework.Mocks.Rest.Guilds; | |||
namespace Discord.Tests.Framework.Routes | |||
{ | |||
public static class Guilds | |||
{ | |||
public static object DiscordApi(string json, IReadOnlyDictionary<string, string> requestHeaders) => | |||
GuildMocks.DiscordApi; | |||
} | |||
} |
@@ -1,37 +1,22 @@ | |||
using UserMocks = Discord.Tests.Framework.Mocks.Rest.Users; | |||
using Newtonsoft.Json; | |||
using System.IO; | |||
using System.Collections.Generic; | |||
using Discord.Net; | |||
using System.Net; | |||
using System.Collections.Immutable; | |||
namespace Discord.Tests.Framework.Routes | |||
{ | |||
public static class Users | |||
{ | |||
public static object Me(string json, IReadOnlyDictionary<string, string> requestHeaders) | |||
{ | |||
Contracts.EnsureAuthorization(requestHeaders); | |||
if (requestHeaders["authorization"] == Contracts.UserToken || requestHeaders["authorization"] == $"Bearer {Contracts.BearerToken}") | |||
return UserMocks.SelfUser; | |||
else | |||
return UserMocks.BotSelfUser; | |||
} | |||
public static object Public(string json, IReadOnlyDictionary<string, string> requestHeaders) | |||
{ | |||
Contracts.EnsureAuthorization(requestHeaders); | |||
return UserMocks.PublicUser; | |||
} | |||
public static object InvalidPublic(string json, IReadOnlyDictionary<string, string> requestHeaders) | |||
{ | |||
Contracts.EnsureAuthorization(requestHeaders); | |||
throw new HttpException(HttpStatusCode.NotFound); | |||
} | |||
public static object Public(string json, IReadOnlyDictionary<string, string> requestHeaders) => | |||
UserMocks.PublicUser; | |||
public static object Query(string json, IReadOnlyDictionary<string, string> requestHeaders) => | |||
ImmutableArray.Create(UserMocks.PublicUser); | |||
} | |||
} |
@@ -0,0 +1,42 @@ | |||
using System.Threading.Tasks; | |||
using Xunit; | |||
using Discord.Rest; | |||
using Contracts = Discord.Tests.Framework.Routes.Contracts; | |||
using Mocks = Discord.Tests.Framework.Mocks.Rest.Guilds; | |||
using RoleMocks = Discord.Tests.Framework.Mocks.Rest.Roles; | |||
namespace Discord.Tests.Rest | |||
{ | |||
public class GuildTests : IClassFixture<RestFixture> | |||
{ | |||
public GuildTests(RestFixture fixture) | |||
{ | |||
_client = fixture.Client; | |||
_client.LoginAsync(TokenType.Bot, Contracts.BotToken).GetAwaiter().GetResult(); | |||
} | |||
private DiscordRestClient _client; | |||
[Fact] | |||
public async Task GetGuild() | |||
{ | |||
var guild = await _client.GetGuildAsync(81384788765712384); | |||
Assert.Equal(Mocks.DiscordApi.Id, guild.Id); | |||
Assert.Equal(Mocks.DiscordApi.Name, guild.Name); | |||
Assert.Equal(Mocks.DiscordApi.OwnerId, guild.OwnerId); | |||
Assert.Equal(Mocks.DiscordApi.MfaLevel, guild.MfaLevel); | |||
Assert.Equal(Mocks.DiscordApi.VerificationLevel, guild.VerificationLevel); | |||
Assert.Equal(Mocks.DiscordApi.Roles.Length, guild.Roles.Count); | |||
Assert.Equal(Mocks.DiscordApi.AFKTimeout, guild.AFKTimeout); | |||
Assert.Equal(Mocks.DiscordApi.DefaultMessageNotifications, guild.DefaultMessageNotifications); | |||
Assert.Equal(Mocks.DiscordApi.EmbedChannelId.GetValueOrDefault(), guild.EmbedChannelId); | |||
Assert.Equal(Mocks.DiscordApi.EmbedEnabled, guild.IsEmbeddable); | |||
} | |||
[Fact] | |||
public async Task GetInvalidGuild() | |||
{ | |||
var guild = await _client.GetGuildAsync(1); | |||
Assert.Null(guild); | |||
} | |||
} | |||
} |
@@ -1,13 +1,8 @@ | |||
using System; | |||
using System.Collections.Generic; | |||
using System.Linq; | |||
using System.Threading.Tasks; | |||
using System.Threading.Tasks; | |||
using Xunit; | |||
using Discord; | |||
using Discord.Rest; | |||
using Contracts = Discord.Tests.Framework.Routes.Contracts; | |||
using Mocks = Discord.Tests.Framework.Mocks.Rest.Users; | |||
using Discord.Net; | |||
namespace Discord.Tests.Rest | |||
{ | |||
@@ -34,7 +29,7 @@ namespace Discord.Tests.Rest | |||
Assert.Equal(Mocks.BotSelfUser.Verified.GetValueOrDefault(), user.IsVerified); | |||
} | |||
[Fact] | |||
public async Task GetUser() | |||
public async Task GetUserById() | |||
{ | |||
var user = await _client.GetUserAsync(66078337084162048); | |||
Assert.Equal(Mocks.PublicUser.Id, user.Id); | |||
@@ -42,10 +37,24 @@ namespace Discord.Tests.Rest | |||
Assert.Equal(Mocks.PublicUser.Discriminator.GetValueOrDefault(), user.Discriminator); | |||
} | |||
[Fact] | |||
public async Task GetInvalidUser() | |||
public async Task GetInvalidUserById() | |||
{ | |||
var user = await _client.GetUserAsync(1); | |||
Assert.Null(user); | |||
} | |||
[Fact] | |||
public async Task GetUserByTag() | |||
{ | |||
var user = await _client.GetUserAsync("foxbot", "0282"); | |||
Assert.Equal(Mocks.PublicUser.Id, user.Id); | |||
Assert.Equal(Mocks.PublicUser.Username.GetValueOrDefault(), user.Username); | |||
Assert.Equal(Mocks.PublicUser.Discriminator.GetValueOrDefault(), user.Discriminator); | |||
} | |||
[Fact] | |||
public async Task GetInvalidUserByTag() | |||
{ | |||
var user = await _client.GetUserAsync("Voltana", "8252"); | |||
Assert.Null(user); | |||
} | |||
} | |||
} |