Skip to content
This repository was archived by the owner on Dec 14, 2017. It is now read-only.

Commit 1e594fe

Browse files
committed
Merge branch 'allow_id'
2 parents 908fb6c + 4e51cff commit 1e594fe

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

src/BrockAllen.MembershipReboot.Test/AccountService/UserAccountServiceTests.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public void CreateAccount_WhitespacePassword_FailsValidation()
374374
Assert.AreEqual(Resources.ValidationMessages.PasswordRequired, ex.Message);
375375
}
376376
}
377-
377+
378378
[TestMethod]
379379
public void CreateAccount_WhitespaceUsername_FailsValidation()
380380
{
@@ -388,6 +388,38 @@ public void CreateAccount_WhitespaceUsername_FailsValidation()
388388
Assert.AreEqual(Resources.ValidationMessages.UsernameRequired, ex.Message);
389389
}
390390
}
391+
392+
[TestMethod]
393+
public void CreateAccount_CanPassID_UsesID()
394+
{
395+
var id = Guid.NewGuid();
396+
var acct = subject.CreateAccount("user", "pass", "[email protected]", id);
397+
Assert.AreEqual(id, acct.ID);
398+
}
399+
400+
[TestMethod]
401+
public void CreateAccount_CanPassCreatedDate_UsesCreatedDate()
402+
{
403+
var created = DateTime.Now;
404+
var acct = subject.CreateAccount("user", "pass", "[email protected]", null, created);
405+
Assert.AreEqual(created, acct.Created);
406+
}
407+
[TestMethod]
408+
public void CreateAccount_FutureCreatedDate_Throws()
409+
{
410+
var created = DateTime.Now.AddDays(1);
411+
try
412+
{
413+
var acct = subject.CreateAccount("user", "pass", "[email protected]", null, created);
414+
Assert.Fail();
415+
}
416+
catch(Exception ex)
417+
{
418+
StringAssert.Contains(ex.Message, "dateCreated");
419+
}
420+
}
421+
422+
391423

392424
[TestMethod]
393425
public void CreateAccount_SettingsRequireEmailIsUsername_UsernameIsEmail()

src/BrockAllen.MembershipReboot/AccountService/UserAccountService.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -379,12 +379,12 @@ protected internal bool MobilePhoneExistsOtherThan(TAccount account, string phon
379379
return false;
380380
}
381381

382-
public virtual TAccount CreateAccount(string username, string password, string email)
382+
public virtual TAccount CreateAccount(string username, string password, string email, Guid? id = null, DateTime? dateCreated = null)
383383
{
384-
return CreateAccount(null, username, password, email);
384+
return CreateAccount(null, username, password, email, id, dateCreated);
385385
}
386386

387-
public virtual TAccount CreateAccount(string tenant, string username, string password, string email)
387+
public virtual TAccount CreateAccount(string tenant, string username, string password, string email, Guid? id = null, DateTime? dateCreated = null)
388388
{
389389
if (Configuration.EmailIsUsername)
390390
{
@@ -401,7 +401,7 @@ public virtual TAccount CreateAccount(string tenant, string username, string pas
401401
Tracing.Information("[UserAccountService.CreateAccount] called: {0}, {1}, {2}", tenant, username, email);
402402

403403
var account = this.userRepository.Create();
404-
Init(account, tenant, username, password, email);
404+
Init(account, tenant, username, password, email, id, dateCreated);
405405

406406
ValidateEmail(account, email);
407407
ValidateUsername(account, username);
@@ -414,7 +414,7 @@ public virtual TAccount CreateAccount(string tenant, string username, string pas
414414
return account;
415415
}
416416

417-
protected void Init(TAccount account, string tenant, string username, string password, string email)
417+
protected void Init(TAccount account, string tenant, string username, string password, string email, Guid? id = null, DateTime? dateCreated = null)
418418
{
419419
Tracing.Information("[UserAccountService.Init] called");
420420

@@ -448,15 +448,22 @@ protected void Init(TAccount account, string tenant, string username, string pas
448448
throw new Exception("Can't call Init if UserAccount is already assigned an ID");
449449
}
450450

451-
account.ID = Guid.NewGuid();
451+
var now = UtcNow;
452+
if (dateCreated > now)
453+
{
454+
Tracing.Error("[UserAccountService.Init] failed -- date created in the future");
455+
throw new Exception("dateCreated can't be in the future");
456+
}
457+
458+
account.ID = id ?? Guid.NewGuid();
452459
account.Tenant = tenant;
453460
account.Username = username;
454461
account.Email = email;
455-
account.Created = UtcNow;
456-
account.LastUpdated = account.Created;
462+
account.Created = dateCreated ?? now;
463+
account.LastUpdated = now;
457464
account.HashedPassword = password != null ?
458465
Configuration.Crypto.HashPassword(password, this.Configuration.PasswordHashingIterationCount) : null;
459-
account.PasswordChanged = password != null ? account.Created : (DateTime?)null;
466+
account.PasswordChanged = password != null ? now : (DateTime?)null;
460467
account.IsAccountVerified = false;
461468
account.AccountTwoFactorAuthMode = TwoFactorAuthMode.None;
462469
account.CurrentTwoFactorAuthStatus = TwoFactorAuthMode.None;

0 commit comments

Comments
 (0)