/
FaceWhitelistTests.cs
193 lines (150 loc) · 7.09 KB
/
FaceWhitelistTests.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Hjerpbakk.Profilebot.Configuration;
using Hjerpbakk.Profilebot.Contracts;
using Hjerpbakk.Profilebot.FaceDetection;
using Hjerpbakk.Profilebot.FaceDetection.Report;
using Microsoft.WindowsAzure.Storage;
using SlackConnector.Models;
using Xunit;
namespace Test.Hjerpbakk.Profilebot.FaceDetection {
/// <summary>
/// If tests fail, verify that the Azure Storage Emulator is installed
/// on the local machine.
/// </summary>
public class FaceWhitelistTests : IClassFixture<BlobStorageFixture> {
// ReSharper disable once UnusedParameter.Local
public FaceWhitelistTests(BlobStorageFixture fixture) { }
[Fact]
public void Ctor() {
Create();
}
[Fact]
public async Task IsUserWhitelisted_Empty_Fails() {
var faceWhitelist = Create();
// ReSharper disable once PossibleNullReferenceException
var exception = await Record.ExceptionAsync(() => faceWhitelist.IsUserWhitelisted(new SlackUser()));
Assert.IsType<ArgumentException>(exception);
}
[Fact]
public async Task IsUserWhitelisted_NonWhitelistedUser_False() {
var faceWhitelist = Create();
var nonWhitelistedUser = new SlackUser {Id = Path.GetRandomFileName()};
Assert.False(await faceWhitelist.IsUserWhitelisted(nonWhitelistedUser));
}
[Fact]
public async Task IsUserWhitelisted_Null_Fails() {
var faceWhitelist = Create();
// ReSharper disable once PossibleNullReferenceException
var exception = await Record.ExceptionAsync(() => faceWhitelist.IsUserWhitelisted(null));
Assert.IsType<ArgumentNullException>(exception);
}
[Fact]
public async Task IsUserWhitelisted_WhitelistedUser_True() {
var faceWhitelist = Create();
var whitelistedUser = new SlackUser {Id = Path.GetRandomFileName()};
await faceWhitelist.WhitelistUser(whitelistedUser);
Assert.True(await faceWhitelist.IsUserWhitelisted(whitelistedUser));
}
[Fact]
public async Task UploadReport() {
var faceWhitelist = Create();
var suspectImage = new Uri("http://" + Path.GetRandomFileName());
var name = Path.GetRandomFileName();
var userId = Path.GetRandomFileName();
await faceWhitelist.UploadReport(new ValidationReport(new ProfileValidationResult(new SlackUser {Id = userId, Name = name}, "errors", suspectImage)));
var storageAccount = CloudStorageAccount.Parse(BlobStorageConfiguration.Local.ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("report");
using (var memoryStream = new MemoryStream()) {
await container.GetBlockBlobReference("report.html").DownloadToStreamAsync(memoryStream);
var report = Encoding.UTF8.GetString(memoryStream.ToArray());
Assert.Contains(suspectImage.AbsolutePath, report);
Assert.Contains(name, report);
Assert.Contains(userId, report);
}
}
[Fact]
public async Task UploadReport_Empty_Fails() {
var faceWhitelist = Create();
// ReSharper disable once PossibleNullReferenceException
var exception = await Record.ExceptionAsync(() => faceWhitelist.UploadReport(null));
Assert.IsType<ArgumentNullException>(exception);
}
[Fact]
public async Task WhitelistUser_Empty_Fails() {
var faceWhitelist = Create();
// ReSharper disable once PossibleNullReferenceException
var exception = await Record.ExceptionAsync(() => faceWhitelist.WhitelistUser(new SlackUser()));
Assert.IsType<ArgumentException>(exception);
}
[Fact]
public async Task WhitelistUser_ExistingUser_RemainsWhitelisted() {
var faceWhitelist = Create();
var userToWhiteList = new SlackUser {Id = Path.GetRandomFileName()};
await faceWhitelist.WhitelistUser(userToWhiteList);
await VerifyUserIsWhiteListed(userToWhiteList);
await faceWhitelist.WhitelistUser(userToWhiteList);
await VerifyUserIsWhiteListed(userToWhiteList);
}
[Fact]
public async Task WhitelistUser_NewUser_IsWhitelisted() {
var faceWhitelist = Create();
var userToWhiteList = new SlackUser {Id = Path.GetRandomFileName()};
await faceWhitelist.WhitelistUser(userToWhiteList);
await VerifyUserIsWhiteListed(userToWhiteList);
}
[Fact]
public async Task GetWhitelistedUsers() {
var faceWhitelist = Create();
var userToWhiteList = new SlackUser {Id = Path.GetRandomFileName()};
await faceWhitelist.WhitelistUser(userToWhiteList);
var whitelistedUsers = await faceWhitelist.GetWhitelistedUsers();
Assert.NotNull(whitelistedUsers.SingleOrDefault(u => u.Id == userToWhiteList.Id));
}
[Fact]
public async Task WhitelistUser_Null_Fails() {
var faceWhitelist = Create();
// ReSharper disable once PossibleNullReferenceException
var exception = await Record.ExceptionAsync(() => faceWhitelist.WhitelistUser(null));
Assert.IsType<ArgumentNullException>(exception);
}
static FaceWhitelist Create() => new FaceWhitelist(BlobStorageConfiguration.Local);
static async Task VerifyUserIsWhiteListed(SlackUser user) {
var storageAccount = CloudStorageAccount.Parse(BlobStorageConfiguration.Local.ConnectionString);
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("whitelist");
using (var memoryStream = new MemoryStream()) {
await container.GetBlockBlobReference(user.Id).DownloadToStreamAsync(memoryStream);
var actualUserId = Encoding.UTF8.GetString(memoryStream.ToArray());
Assert.Equal(user.Id, actualUserId);
}
}
}
public class BlobStorageFixture : IDisposable {
readonly Process process;
public BlobStorageFixture() {
process = new Process {
StartInfo = {
UseShellExecute = false,
FileName = @"C:\Program Files (x86)\Microsoft SDKs\Azure\Storage Emulator\AzureStorageEmulator.exe",
}
};
StartAndWaitForExit("stop");
StartAndWaitForExit("clear all");
StartAndWaitForExit("start");
}
public void Dispose() {
StartAndWaitForExit("stop");
}
void StartAndWaitForExit(string arguments) {
process.StartInfo.Arguments = arguments;
process.Start();
process.WaitForExit(10000);
}
}
}