-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllVenues.cs
76 lines (62 loc) · 2.87 KB
/
AllVenues.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using FFXIVVenues.VenueTests.Discord;
using NUnit.Framework;
using System.Collections;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using VenueModels.V2021;
namespace FFXIVVenues.VenueTests
{
public class AllVenues
{
private static Regex _discordPattern = new Regex(@"(https?:\/\/)?(www\.)?((discord(app)?(\.com|\.io)(\/invite)?)|(discord\.gg))\/(\w+)");
private static Venue[] _venues;
private HttpClient _client;
static AllVenues()
{
var request = new HttpClient().GetAsync("https://raw.githubusercontent.com/FFXIVVenues/ffxiv-venues-web/master/src/venues.json").Result;
_venues = JsonSerializer.Deserialize<Venue[]>(request.Content.ReadAsStream());
}
public static IEnumerable GetData() =>
_venues.Where(v => v != null).Select(v => new TestCaseData(v).SetArgDisplayNames(v.name));
[OneTimeSetUp]
public void OneTimeSetup()
{
this._client = new HttpClient();
}
[Test]
[TestCaseSource("GetData")]
public async Task HaveValidWebsites(Venue venue)
{
if (venue.website == null)
Assert.Ignore($"No website link for {venue.name}.");
var response = await this._client.GetAsync(venue.website);
Assert.IsTrue(response.IsSuccessStatusCode, $"The website link for {venue.name} is giving a {response.StatusCode} ({venue.website}).");
}
[Test]
[TestCaseSource("GetData")]
public async Task HaveValidDiscord(Venue venue)
{
if (venue.discord == null)
Assert.Ignore($"No discord link for {venue.name}.");
await Task.Delay(5000);
var match = _discordPattern.Match(venue.discord);
if (!match.Success)
Assert.Ignore($"The discord link for {venue.name} is not a standard link, skipped.");
var inviteCode = match.Groups[9].ToString();
var responseMessage = await this._client.GetAsync($"https://discordapp.com/api/invite/{inviteCode}");
Assert.IsTrue(responseMessage.StatusCode != HttpStatusCode.NotFound, $"The discord link for {venue.name} is invalid ({venue.discord}).");
if (!responseMessage.IsSuccessStatusCode)
{
Assert.Warn($"Could not query validity of discord link for {venue.name}.");
return;
}
var response = await responseMessage.Content.ReadAsStreamAsync();
var invite = await JsonSerializer.DeserializeAsync<Invite>(response);
Assert.IsTrue(invite.Expires_at == null, $"The discord link for {venue.name} expires at {invite.Expires_at}.");
}
}
}