Skip to content

Commit 652d267

Browse files
committed
Working on Logs - Part 4
1 parent 7101668 commit 652d267

7 files changed

Lines changed: 73 additions & 101 deletions

File tree

Core/Models/Database.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ public static IDatabase Create(ICryptographyCenter cryptographicCenter,
279279
{
280280
Database = database,
281281
PrivateKey = privateKey,
282-
ItemId = cryptographicCenter.GetHash(username),
282+
ItemId = "U" + cryptographicCenter.GetHash(username),
283283
Username = username,
284284
Passkeys = [.. passkeys],
285285
};
@@ -453,22 +453,7 @@ private Warning[] _lookAtLogWarnings()
453453
if (User is null) throw new NullReferenceException(nameof(User));
454454
if (Logs.Logs is null) throw new NullReferenceException(nameof(Logs.Logs));
455455

456-
List<Log> logs = [.. Logs.Logs.Cast<Log>()];
457-
458-
//for (int i = 0; i < logs.Count && logs[i].EventType != LogEventType.UserLoggedIn; i++)
459-
//{
460-
// if (!logs[i].NeedsReview
461-
// || (logs[i].EventType != LogEventType.MergeAndSaveThenRemoveAutoSaveFile
462-
// && logs[i].EventType != LogEventType.MergeWithoutSavingAndKeepAutoSaveFile
463-
// && logs[i].EventType != LogEventType.DontMergeAndRemoveAutoSaveFile
464-
// && logs[i].EventType != LogEventType.DontMergeAndKeepAutoSaveFile))
465-
// {
466-
// logs.RemoveAt(i);
467-
// i--;
468-
// }
469-
//}
470-
471-
return [new Warning([.. logs.Where(x => x.NeedsReview)])];
456+
return [new Warning([.. Logs.Logs.Where(x => x.NeedsReview).Cast<Log>()])];
472457
}
473458

474459
private Warning[] _lookAtPasswordUpdateReminderWarnings()

Core/Models/Service.cs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public IAccount AddAccount(IEnumerable<string> identifiers)
9090
void IService.DeleteAccount(IAccount account)
9191
{
9292
Account accountToRemove = Accounts.FirstOrDefault(x => x.ItemId == account.ItemId)
93-
?? throw new KeyNotFoundException($"The '{account.ItemId}' account was not found into the '{ItemId}' service");
93+
?? throw new KeyNotFoundException($"The {account}' was not found into the {this}'s accounts list");
9494

9595
_ = Accounts.Remove(Database.AutoSave.DeleteValue(ItemId, readableValue: accountToRemove.ToString(), needsReview: true, accountToRemove));
9696
}
@@ -122,24 +122,6 @@ internal User User
122122
public string Notes { get; set; } = string.Empty;
123123

124124
public void Apply(Change change)
125-
{
126-
switch (change.ItemId.Length / Database.CryptographyCenter.HashLength)
127-
{
128-
case 2:
129-
_apply(change);
130-
break;
131-
case 3:
132-
Account account = Accounts.FirstOrDefault(x => change.ItemId.StartsWith(x.ItemId))
133-
?? throw new KeyNotFoundException($"The '{change.ItemId}' account was not found into the '{ItemId}' service");
134-
135-
account.Apply(change);
136-
break;
137-
default:
138-
throw new InvalidDataException("ItemId not valid");
139-
}
140-
}
141-
142-
private void _apply(Change change)
143125
{
144126
switch (change.ActionType)
145127
{

Core/Models/User.cs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ IService IUser.AddService(string serviceName)
138138
void IUser.DeleteService(IService service)
139139
{
140140
Service serviceToRemove = Services.FirstOrDefault(x => x.ItemId == service.ItemId)
141-
?? throw new KeyNotFoundException($"The '{service.ItemId}' service was not found into the '{ItemId}' user");
141+
?? throw new KeyNotFoundException($"The {service} was not found into the {this}'s services list");
142142

143143
_ = Services.Remove(Database.AutoSave.DeleteValue(ItemId, readableValue: serviceToRemove.ToString(), needsReview: true, value: serviceToRemove));
144144
}
@@ -230,18 +230,23 @@ public void ResetTimer()
230230

231231
public void Apply(Change change)
232232
{
233-
switch (change.ItemId.Length / Database.CryptographyCenter.HashLength)
233+
switch (change.ItemId[0])
234234
{
235-
case 1:
235+
case 'U':
236236
_apply(change);
237237
break;
238-
case 2:
239-
case 3:
240-
Service service = Services.FirstOrDefault(x => change.ItemId.StartsWith(x.ItemId))
241-
?? throw new KeyNotFoundException($"The '{change.ItemId[..(2 * Database.CryptographyCenter.HashLength)]}' service was not found into the '{ItemId}' user");
238+
case 'S':
239+
Service service = Services.FirstOrDefault(x => change.ItemId == x.ItemId)
240+
?? throw new KeyNotFoundException($"The Service '{change.ItemId}' was not found into the {this}'s services list");
242241

243242
service.Apply(change);
244243
break;
244+
case 'A':
245+
Account account = Services.SelectMany(x => x.Accounts).FirstOrDefault(x => change.ItemId == x.ItemId)
246+
?? throw new KeyNotFoundException($"The Account {change.ItemId}' was not found into the {this}'s accounts list");
247+
248+
account.Apply(change);
249+
break;
245250
default:
246251
throw new InvalidDataException("ItemId not valid");
247252
}

UnitTests/Models/AccountUnitTests.cs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,23 @@ public void Case01_AddAccountUpdateSaved()
5454
// When
5555
account.Label = newAccountLabel;
5656
account.Label = newAccountLabel;
57-
expectedLogs.Push($"Information : {account}'s label has been set to {newAccountLabel}");
57+
expectedLogs.Push($"Information : Account {oldAccountLabel} ({string.Join(", ", oldIdentifiers)})'s label has been set to {newAccountLabel}");
5858
account.Identifiers = newIdentifiers;
5959
account.Identifiers = newIdentifiers;
60-
expectedLogs.Push($"Warning : Account {account}'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
61-
expectedLogWarnings.Push($"Warning : Account {account}'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
60+
expectedLogs.Push($"Warning : Account {newAccountLabel} ({string.Join(", ", oldIdentifiers)})'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
61+
expectedLogWarnings.Push($"Warning : Account {newAccountLabel} ({string.Join(", ", oldIdentifiers)})'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
6262
account.Password = newPassword;
6363
account.Password = newPassword;
64-
expectedLogs.Push($"Warning : Account {account}'s password has been updated");
65-
expectedLogWarnings.Push($"Warning : Account {account}'s password has been updated");
64+
expectedLogs.Push($"Warning : {account}'s password has been updated");
65+
expectedLogWarnings.Push($"Warning : {account}'s password has been updated");
6666
account.Notes = notes;
6767
account.Notes = notes;
68-
expectedLogs.Push($"Information : Account {account}'s notes has been set to {notes}");
68+
expectedLogs.Push($"Information : {account}'s notes has been set to {notes}");
6969
account.PasswordUpdateReminderDelay = passwordUpdateReminderDelay;
70-
expectedLogs.Push($"Information : Account {account}'s password update reminder delay has been set to {passwordUpdateReminderDelay}");
70+
expectedLogs.Push($"Information : {account}'s password update reminder delay has been set to {passwordUpdateReminderDelay}");
7171
account.Options = options;
7272
account.Options = options;
73-
expectedLogs.Push($"Information : Account {account}'s options has been set to {options}");
73+
expectedLogs.Push($"Information : {account}'s options has been set to {options}");
7474

7575
// Then
7676
databaseCreated.User.HasChanged().Should().BeTrue();
@@ -161,23 +161,23 @@ public void Case02_AddAccountUpdateAutoSave()
161161
// When
162162
account.Label = newAccountLabel;
163163
account.Label = newAccountLabel;
164-
expectedLogs.Push($"Information : {account}'s label has been set to {newAccountLabel}");
164+
expectedLogs.Push($"Information : Account {oldAccountLabel} ({string.Join(", ", oldIdentifiers)})'s label has been set to {newAccountLabel}");
165165
account.Identifiers = newIdentifiers;
166166
account.Identifiers = newIdentifiers;
167-
expectedLogs.Push($"Warning : Account {account}'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
168-
expectedLogWarnings.Push($"Warning : Account {account}'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
167+
expectedLogs.Push($"Warning : Account {newAccountLabel} ({string.Join(", ", oldIdentifiers)})'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
168+
expectedLogWarnings.Push($"Warning : Account {newAccountLabel} ({string.Join(", ", oldIdentifiers)})'s identifiers has been set to ({string.Join(", ", newIdentifiers)})");
169169
account.Password = newPassword;
170170
account.Password = newPassword;
171-
expectedLogs.Push($"Warning : Account {account}'s password has been updated");
172-
expectedLogWarnings.Push($"Warning : Account {account}'s password has been updated");
171+
expectedLogs.Push($"Warning : {account}'s password has been updated");
172+
expectedLogWarnings.Push($"Warning : {account}'s password has been updated");
173173
account.Notes = notes;
174174
account.Notes = notes;
175-
expectedLogs.Push($"Information : Account {account}'s notes has been set to {notes}");
175+
expectedLogs.Push($"Information : {account}'s notes has been set to {notes}");
176176
account.PasswordUpdateReminderDelay = passwordUpdateReminderDelay;
177-
expectedLogs.Push($"Information : Account {account}'s password update reminder delay has been set to {passwordUpdateReminderDelay}");
177+
expectedLogs.Push($"Information : {account}'s password update reminder delay has been set to {passwordUpdateReminderDelay}");
178178
account.Options = options;
179179
account.Options = options;
180-
expectedLogs.Push($"Information : Account {account}'s options has been set to {options}");
180+
expectedLogs.Push($"Information : {account}'s options has been set to {options}");
181181

182182
databaseCreated.Close();
183183
expectedLogs.Push($"Warning : User {username} logged out without saving");

UnitTests/Models/DatabaseUnitTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void Case01_DatabaseCreationOpenDelete()
8282

8383
// When
8484
IDatabase databaseCreated = UnitTestsHelper.CreateTestDatabase(passkeys);
85-
expectedLogs.Push($"Information : User {username}'s database created");
85+
expectedLogs.Push($"Information : {databaseCreated.User}'s database created");
8686

8787
// Then
8888
_ = databaseCreated.DatabaseFile.Should().Be(databaseFile);
@@ -105,8 +105,8 @@ public void Case01_DatabaseCreationOpenDelete()
105105

106106
// When
107107
IDatabase databaseLoaded = UnitTestsHelper.OpenTestDatabase(passkeys, out _);
108-
expectedLogs.Push($"Information : User {username}'s database opened");
109-
expectedLogs.Push($"Information : User {username} logged in");
108+
expectedLogs.Push($"Information : {databaseLoaded.User}'s database opened");
109+
expectedLogs.Push($"Information : {databaseLoaded.User} logged in");
110110

111111
// Then
112112
_ = databaseLoaded.Should().NotBeNull();

UnitTests/Models/ServiceUnitTests.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public void Case01_AddServiceUpdateSaved()
3131

3232
// When
3333
IService service = databaseCreated.User.AddService(oldServiceName);
34-
expectedLogs.Push($"Information : Service {oldServiceName} has been added to User {username}");
34+
expectedLogs.Push($"Information : {service} has been added to User {username}");
3535

3636
// Then
3737
databaseCreated.User.HasChanged().Should().BeTrue();
@@ -45,10 +45,10 @@ public void Case01_AddServiceUpdateSaved()
4545
expectedLogWarnings.Push($"Warning : Service {oldServiceName}'s service name has been set to {newServiceName}");
4646
service.Url = url;
4747
service.Url = url;
48-
expectedLogs.Push($"Information : Service {newServiceName}'s url has been set to {url}");
48+
expectedLogs.Push($"Information : {service}'s url has been set to {url}");
4949
service.Notes = notes;
5050
service.Notes = notes;
51-
expectedLogs.Push($"Information : Service {newServiceName}'s notes has been set to {notes}");
51+
expectedLogs.Push($"Information : {service}'s notes has been set to {notes}");
5252

5353
// Then
5454
databaseCreated.User.HasChanged().Should().BeTrue();
@@ -109,7 +109,7 @@ public void Case02_AddServiceUpdateAutoSave()
109109

110110
// When
111111
IService service = databaseCreated.User.AddService(oldServiceName);
112-
expectedLogs.Push($"Information : Service {oldServiceName} has been added to User {username}");
112+
expectedLogs.Push($"Information : {service} has been added to User {username}");
113113

114114
// Then
115115
_ = databaseCreated.User.Services.Length.Should().Be(1);
@@ -121,10 +121,10 @@ public void Case02_AddServiceUpdateAutoSave()
121121
expectedLogWarnings.Push($"Warning : Service {oldServiceName}'s service name has been set to {newServiceName}");
122122
service.Url = url;
123123
service.Url = url;
124-
expectedLogs.Push($"Information : Service {newServiceName}'s url has been set to {url}");
124+
expectedLogs.Push($"Information : {service}'s url has been set to {url}");
125125
service.Notes = notes;
126126
service.Notes = notes;
127-
expectedLogs.Push($"Information : Service {newServiceName}'s notes has been set to {notes}");
127+
expectedLogs.Push($"Information : {service}'s notes has been set to {notes}");
128128

129129
databaseCreated.Close();
130130
expectedLogs.Push($"Warning : User {username} logged out without saving");

0 commit comments

Comments
 (0)