/
RepositoryTestsBase.cs
329 lines (280 loc) · 15.5 KB
/
RepositoryTestsBase.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using PassFruit.Contracts;
using PassFruit.Tests.FakeData;
namespace PassFruit.Tests {
[TestFixture]
public abstract class RepositoryTestsBase {
protected abstract IRepository GetNewRepositoryWithFakeData();
protected abstract IRepository GetReloadedRepository();
private void TestWithUnsavedFakeData(Action<IRepository> test) {
test(GetNewRepositoryWithFakeData());
}
private void TestWithReloadedRepository(Action<IRepository> test) {
test(GetReloadedRepository());
}
private void TestWithPrepopulatedReloadedRepository(Action<IRepository> test) {
GetNewRepositoryWithFakeData().SaveAll();
test(GetReloadedRepository());
}
private void TestWithBothRepositories(Action<IRepository> test) {
test(GetNewRepositoryWithFakeData());
test(GetReloadedRepository());
}
[Test]
public void When_An_Account_Is_Loaded_The_Correct_One_Should_Be_Returned() {
// Given
IAccount facebookAccount = null;
IAccount twitterAccountByUserName = null;
IAccount twitterAccountByEmail = null;
IAccount gmailAccount = null;
const string testFacebookEmail = FakeDataGenerator.FacebookEmail;
const string testTwitterAccount = FakeDataGenerator.TwitterUserName;
const string testTwitterEmail = FakeDataGenerator.TwitterEmail;
const string testGoogleEmail = FakeDataGenerator.Google1Email;
// When
Action<IRepository> actLoadAccounts = repository => {
facebookAccount = repository.Accounts.GetByEmail(testFacebookEmail).FirstOrDefault();
twitterAccountByUserName = repository.Accounts.GetByUserName(testTwitterAccount).FirstOrDefault();
twitterAccountByEmail = repository.Accounts.GetByEmail(testTwitterEmail).FirstOrDefault();
gmailAccount = repository.Accounts.GetByEmail(testGoogleEmail).FirstOrDefault();
};
// Then
TestWithBothRepositories(repository => {
actLoadAccounts(repository);
facebookAccount.Should().NotBeNull();
facebookAccount.GetDefaultField(FieldTypeKey.Email).Value.Should().Be(testFacebookEmail);
facebookAccount.Provider.Should().NotBeNull();
facebookAccount.Provider.Key.Should().Be(FakeDataGenerator.FacebookProviderKey);
twitterAccountByUserName.Should().NotBeNull();
twitterAccountByUserName.GetDefaultField(FieldTypeKey.UserName).Value.Should().Be(testTwitterAccount);
twitterAccountByUserName.Provider.Should().NotBeNull();
twitterAccountByUserName.Provider.Key.Should().Be(FakeDataGenerator.TwitterProviderKey);
twitterAccountByEmail.Should().NotBeNull();
twitterAccountByEmail.GetDefaultField(FieldTypeKey.Email).Value.Should().Be(testTwitterEmail);
twitterAccountByEmail.Provider.Should().NotBeNull();
twitterAccountByEmail.Provider.Key.Should().Be(FakeDataGenerator.TwitterProviderKey);
gmailAccount.Should().NotBeNull();
gmailAccount.GetDefaultField(FieldTypeKey.Email).Value.Should().Be(testGoogleEmail);
gmailAccount.Provider.Should().NotBeNull();
gmailAccount.Provider.Key.Should().Be(FakeDataGenerator.GoogleProviderKey);
});
}
[Test]
public void When_An_Account_Is_Added_Then_The_Number_Of_Accounts_Should_Increase() {
// Given
const string newEmail = "testFacebookTemp@tin.it";
var originalAccountCount = 0;
IAccount facebookAccount = null;
// When
Action<IRepository> actLoadAccount = repository => {
originalAccountCount = repository.Accounts.Count();
facebookAccount = repository.Accounts.Create(FakeDataGenerator.GenericProviderKey);
};
Action<IRepository> actEditAccount = repository =>
facebookAccount.SetField(FieldTypeKey.Email, newEmail);
Action<IRepository> actAddAccount = repository =>
repository.Accounts.Add(facebookAccount);
Action<IRepository> actRepositorySaveAll = repository =>
repository.SaveAll();
// Then
TestWithBothRepositories(repository => {
actLoadAccount(repository);
actEditAccount(repository);
facebookAccount.IsDirty.Should().BeTrue();
repository.Accounts.Count.Should().Be(originalAccountCount);
actAddAccount(repository);
actRepositorySaveAll(repository);
facebookAccount.IsDirty.Should().BeFalse();
repository.Accounts.Count.Should().Be(originalAccountCount + 1);
});
}
[Test]
public void When_An_Account_Is_Deleted_Then_The_Number_Of_Accounts_Should_Decrease() {
// Given
var originalAccountCount = 0;
IAccount facebookAccount = null;
IAccount deletedAccount = null;
var facebookAccountId = Guid.Empty;
// When
Action<IRepository> actLoadAccount = repository => {
originalAccountCount = repository.Accounts.Count();
facebookAccount = repository.Accounts.GetByEmail(FakeDataGenerator.FacebookEmail).First();
facebookAccountId = facebookAccount.Id;
};
Action<IRepository> actRemoveAccount = repository =>
repository.Accounts.Remove(facebookAccount);
Action<IRepository> actLoadDeletedAccount = repository =>
deletedAccount = repository.Accounts.First(a => a is DeletedAccount);
// Then
TestWithPrepopulatedReloadedRepository(repository => {
actLoadAccount(repository);
for (int i = 0; i < 3; i++) {
if (i < 2) {
actRemoveAccount(repository);
} else if (i == 2) {
actLoadDeletedAccount(repository);
repository.Accounts.Remove(deletedAccount);
}
repository.Accounts.Count().Should().Be(originalAccountCount);
repository.Accounts.Should().Contain(a => a is DeletedAccount);
repository.Accounts.Count(a => a is DeletedAccount).Should().Be(1);
repository.Accounts.First(a => a is DeletedAccount).IsDirty.Should().BeTrue();
repository.IsDirty().Should().BeTrue();
}
repository.GetPassword(facebookAccount.Id).Should().BeBlank();
repository.IsDirty().Should().BeTrue();
repository.SaveAll();
facebookAccount.GetPassword().Should().BeBlank();
repository.GetPassword(facebookAccount.Id).Should().BeBlank();
repository.IsDirty().Should().BeFalse();
repository.Accounts.GetByEmail(FakeDataGenerator.FacebookEmail).Should().BeEmpty();
repository.Accounts.Count().Should().Be(originalAccountCount - 1);
repository.Accounts.Should().NotContain(a => a is DeletedAccount);
});
TestWithReloadedRepository(repository => {
repository.IsDirty().Should().BeFalse();
repository.Accounts.GetByEmail(FakeDataGenerator.FacebookEmail).Should().BeEmpty();
repository.Accounts.Count().Should().Be(originalAccountCount - 1);
repository.GetPassword(facebookAccountId).Should().BeBlank();
var deletedAccounts = repository.GetDeletedAccounts();
deletedAccounts.Any().Should().BeTrue();
deletedAccounts.Should().Contain(account => account.Id == facebookAccountId);
});
}
[Test]
public void When_An_Account_Is_Edited_Then_The_Correct_Data_Should_Be_Retrieved() {
// Given
const string editedUser = "edItedUsEr";
const string editedEmail = "editedEmail@twitter.example";
const string editedNote = "edited note text bla bla bla! ¬!\"£$%^&*()_+=-`[]#'/.,<>?|\\";
IAccount accountWithUserName = null;
var originalId = Guid.Empty;
// When
Action<IRepository> actLoadAndEditAccount = repository => {
accountWithUserName = repository.Accounts.GetByUserName(FakeDataGenerator.TwitterUserName).First();
originalId = accountWithUserName.Id;
accountWithUserName.SetField(FieldTypeKey.UserName, editedUser);
accountWithUserName.SetField(FieldTypeKey.Email, editedEmail);
accountWithUserName.Notes = editedNote;
accountWithUserName.Save();
};
// Then
TestWithBothRepositories(repository => {
if (accountWithUserName == null) {
actLoadAndEditAccount(repository);
}
var retrievedAccount = repository.Accounts.GetByUserName(editedUser).First();
retrievedAccount.Id.Should().Be(originalId);
retrievedAccount.GetDefaultField(FieldTypeKey.UserName).Value.Should().Be(editedUser);
retrievedAccount.GetDefaultField(FieldTypeKey.Email).Value.Should().Be(editedEmail);
retrievedAccount.Notes.Should().Be(editedNote);
});
}
[Test]
public void When_A_Tag_Is_Added_Then_The_Number_Of_Tags_Should_Increase() {
// Given
IAccount twitterAccount = null;
var originalTotalTagCount = -1;
var originalAccountTagCount = -1;
const string testTagA = "test-tag-A";
const string testTagB = "test-tag-B";
const string testNonValidTag = "unvalid tag";
// When
Action<IAccount> actAddTag = account => account.Tags.Add(testTagA);
Action<IAccount> actSave = account => account.Save();
Action<IRepository> actLoadAccount = repository =>
twitterAccount = repository.Accounts.GetByUserName(FakeDataGenerator.TwitterUserName).First();
// Then
TestWithUnsavedFakeData(repository => {
actLoadAccount(repository);
originalTotalTagCount = repository.GetAllTags().Count();
originalAccountTagCount = twitterAccount.Tags.Count();
twitterAccount.IsDirty.Should().BeFalse();
actAddTag(twitterAccount);
twitterAccount.IsDirty.Should().BeTrue();
repository.GetAllTags().Count().Should().Be(originalTotalTagCount + 1);
repository.GetAllTags().Count().Should().BeGreaterOrEqualTo(twitterAccount.Tags.Count());
actSave(twitterAccount);
});
TestWithReloadedRepository(repository => {
actLoadAccount(repository);
twitterAccount.IsDirty.Should().BeFalse();
twitterAccount.Tags.Count().Should().Be(originalAccountTagCount + 1);
repository.GetAllTags().Count().Should().Be(originalTotalTagCount + 1);
repository.GetAllTags().Any(tag => tag.Key.Equals(testTagA, StringComparison.OrdinalIgnoreCase)).Should().BeTrue();
repository.GetAllTags().Any(tag => tag.Key.Equals(testTagB, StringComparison.OrdinalIgnoreCase)).Should().BeFalse();
repository.GetAllTags().Count().Should().BeGreaterOrEqualTo(twitterAccount.Tags.Count());
var accounts = repository.GetAllTags().First(tag => tag.Key.Equals(testTagA, StringComparison.OrdinalIgnoreCase)).Accounts;
accounts.Should().Contain(account => account.Equals(twitterAccount));
});
}
[Test]
public void When_A_Tag_Is_Deleted_Then_The_Number_Of_Tags_Should_Decrease() {
// Given
IAccount googleAccount = null;
var originalTotalTagCount = -1;
var originalAccountTagCount = -1;
// When
Action<IRepository> actLoadAccount = repository =>
googleAccount = repository.Accounts.GetByEmail(FakeDataGenerator.Google1Email).First();
Action actDeleteTag = () => {
googleAccount.Tags.Remove(FakeDataGenerator.Tag1);
googleAccount.Tags.Remove(FakeDataGenerator.Tag2);
googleAccount.Tags.Remove(FakeDataGenerator.Tag3);
};
Action actSave = () => googleAccount.Save();
// Then
TestWithUnsavedFakeData(repository => {
actLoadAccount(repository);
originalTotalTagCount = repository.GetAllTags().Count();
originalAccountTagCount = googleAccount.Tags.Count();
googleAccount.IsDirty.Should().BeFalse();
actDeleteTag();
googleAccount.IsDirty.Should().BeTrue();
googleAccount.Tags.Count().Should().Be(0);
repository.GetAllTags().Count().Should().BeGreaterOrEqualTo(googleAccount.Tags.Count());
actSave();
});
TestWithReloadedRepository(repository => {
actLoadAccount(repository);
googleAccount.IsDirty.Should().BeFalse();
googleAccount.Tags.Count().Should().Be(0);
repository.GetAllTags().Count().Should().BeGreaterOrEqualTo(0);
repository.GetAllTags().Any(tag => tag.Key.Equals(FakeDataGenerator.Tag1, StringComparison.OrdinalIgnoreCase)).Should().BeTrue();
});
}
[Test]
public void When_A_Password_Is_Changed_Then_The_Account_Should_Not_Be_Dirty_And_The_Password_Shold_Be_Saved() {
// Given
var repository = GetNewRepositoryWithFakeData();
var account = repository.Accounts.GetByUserName(FakeDataGenerator.TwitterUserName).First();
var originalDefaultPassword = account.GetPassword();
var originalCustomPassword = account.GetPassword("custom");
var newDefaultPassword = "new password 123131 \"\"\"\"$£&!£&!\'\'\'\'|!£!><><>><";
var newCustomPassword = "new Pasw0rd";
var newEmail = "test@example.org";
// When
var actChangeDefaultPassword = new Action(() => account.SetPassword(newDefaultPassword));
var actChangeCustomPassword = new Action(() => account.SetPassword(newCustomPassword, "CuStoM"));
var actChangeEmail = new Action(() => account.SetField(FieldTypeKey.Email, newEmail));
var actSave = new Action(account.Save);
// Then
account.IsDirty.Should().BeFalse();
account.GetPassword().Should().Be(originalDefaultPassword);
account.GetPassword("CUSToM").Should().Be(originalCustomPassword);
actChangeDefaultPassword();
actChangeCustomPassword();
account.IsDirty.Should().BeFalse(); // If a password is changed the dirty flag should not be set.
actChangeEmail();
account.IsDirty.Should().BeTrue();
actSave();
account.IsDirty.Should().BeFalse();
account.GetPassword().Should().Be(newDefaultPassword);
account.GetPassword("CUStom").Should().Be(newCustomPassword);
account.GetFieldsByKey(FieldTypeKey.Email).First().Value.Should().Be(newEmail);
}
}
}