unit UTestAccount; {$mode objfpc}{$H+} interface uses Classes, SysUtils, DOM, XMLRead, XMLWrite, fpcunit, testregistry, UEnums, UInterfaces, UAccount, UConfig, UTestConfig; type TTestAccounts = class(TTestCase) private function BuildAccount: TAccount; function AccountToXML(AAccount: TAccount): string; protected procedure SetUp; override; procedure TearDown; override; published procedure CreateAndCheckPassword; procedure UpdatePasswordInvalidatesOldPassword; procedure HashDoesNotContainPassword; procedure FindInList; procedure SerializeDeserializeRoundtrip; end; implementation uses MD5; type TDummyOwner = class(TObject, IInvalidate) procedure Invalidate; end; procedure TDummyOwner.Invalidate; begin //notify chain ends here in tests end; var configPath: string; dummyOwner: TDummyOwner; procedure TTestAccounts.SetUp; begin //account serialization validates regions against the global config configPath := WriteTestConfigFile; Config := TConfig.Create(configPath); dummyOwner := TDummyOwner.Create; end; procedure TTestAccounts.TearDown; begin FreeAndNil(Config); dummyOwner.Free; DeleteFile(configPath); end; function TTestAccounts.BuildAccount: TAccount; var regions: TStringList; begin regions := TStringList.Create; regions.Add('britain'); regions.Add('yew'); Result := TAccount.Create(dummyOwner, 'alice', 'secret', alNormal, regions); end; function TTestAccounts.AccountToXML(AAccount: TAccount): string; var doc: TXMLDocument; root: TDOMElement; stream: TStringStream; begin doc := TXMLDocument.Create; try root := doc.CreateElement('accounts'); doc.AppendChild(root); AAccount.Serialize(root); stream := TStringStream.Create(''); try WriteXMLFile(doc, stream); Result := stream.DataString; finally stream.Free; end; finally doc.Free; end; end; procedure TTestAccounts.CreateAndCheckPassword; var account: TAccount; begin account := BuildAccount; try AssertEquals('name', 'alice', account.Name); AssertTrue('correct password accepted', account.CheckPassword('secret')); AssertFalse('wrong password rejected', account.CheckPassword('wrong')); finally account.Free; end; end; procedure TTestAccounts.UpdatePasswordInvalidatesOldPassword; var account: TAccount; begin account := BuildAccount; try account.UpdatePassword('newpass'); AssertFalse('old password must stop working', account.CheckPassword('secret')); AssertTrue('new password accepted', account.CheckPassword('newpass')); finally account.Free; end; end; procedure TTestAccounts.HashDoesNotContainPassword; var account: TAccount; begin account := BuildAccount; try AssertTrue('stored hash must not contain the plain password', Pos('secret', account.PasswordHash) = 0); finally account.Free; end; end; procedure TTestAccounts.FindInList; var list: TAccountList; account: TAccount; begin list := TAccountList.Create(dummyOwner); try account := BuildAccount; list.Add(account); AssertSame('find existing', account, list.Find('alice')); AssertEquals('index', 0, list.IndexOf('alice')); AssertTrue('missing is nil', list.Find('bob') = nil); finally list.Free; end; end; procedure TTestAccounts.SerializeDeserializeRoundtrip; var account, loaded: TAccount; xml: string; stream: TStringStream; doc: TXMLDocument; begin account := BuildAccount; try xml := AccountToXML(account); stream := TStringStream.Create(xml); try ReadXMLFile(doc, stream); try loaded := TAccount.Deserialize(nil, doc.DocumentElement); try AssertEquals('name survives', 'alice', loaded.Name); AssertTrue('password hash survives', loaded.CheckPassword('secret')); AssertEquals('access level survives', Ord(alNormal), Ord(loaded.AccessLevel)); AssertEquals('regions survive', 2, loaded.Regions.Count); AssertEquals('region name', 'britain', loaded.Regions[0]); finally loaded.Free; end; finally doc.Free; end; finally stream.Free; end; finally account.Free; end; end; initialization RegisterTests([TTestAccounts]); end.