Exemplo n.º 1
0
        public void deleted_account_should_not_persist_file()
        {
            Account acct;

            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile))
            {
                acct = p.AccountsSettings.Upsert("foo", "us");
                p.AccountsSettings.Accounts.Count.Should().Be(1);
                acct.AccountName = "old";
            }

            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile))
            {
                p.AccountsSettings.Delete(acct);
                p.AccountsSettings.Accounts.Count.Should().Be(0);
            }

            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile))
            {
                File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);

                acct.AccountName = "new";

                File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);
            }
        }
Exemplo n.º 2
0
        public void save_1_account()
        {
            // create initial file
            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }

            // load file. create account
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                var idIn   = new Identity(usLocale);
                var acctIn = new Account("a0")
                {
                    AccountName = "n0", IdentityTokens = idIn
                };

                p.AccountsSettings.Add(acctIn);
            }

            // re-load file. ensure account still exists
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                p.AccountsSettings.Accounts.Count.Should().Be(1);
                var acct0 = p.AccountsSettings.Accounts[0];
                acct0.AccountName.Should().Be("n0");
                acct0.Locale.CountryCode.Should().Be("us");
            }
        }
Exemplo n.º 3
0
        public void update_Account_field_just_added()
        {
            // create initial file
            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }

            // load file. create 2 accounts
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                var id1   = new Identity(usLocale);
                var acct1 = new Account("a0")
                {
                    AccountName = "n0", IdentityTokens = id1
                };
                p.AccountsSettings.Add(acct1);

                // update just-added item. note: this is different than the subscription which happens on initial collection load. ensure this works also
                acct1.AccountName = "new";
            }

            // verify save property
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                var acct0 = p.AccountsSettings.Accounts[0];
                acct0.AccountName.Should().Be("new");
            }
        }
Exemplo n.º 4
0
        public void create_file()
        {
            File.Exists(TestFile).Should().BeFalse();
            var accountsSettings = new AccountsSettings();

            _ = new AccountsSettingsPersister(accountsSettings, TestFile);
            File.Exists(TestFile).Should().BeTrue();
            File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);
        }
Exemplo n.º 5
0
        public void overwrite_existing_file()
        {
            File.Exists(TestFile).Should().BeFalse();
            WriteToTestFile("foo");
            File.Exists(TestFile).Should().BeTrue();

            var accountsSettings = new AccountsSettings();

            _ = new AccountsSettingsPersister(accountsSettings, TestFile);
            File.Exists(TestFile).Should().BeTrue();
            File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);
        }
Exemplo n.º 6
0
        public void update_identity_field()
        {
            // create initial file
            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }

            // load file. create 2 accounts
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                var id1   = new Identity(usLocale);
                var acct1 = new Account("a0")
                {
                    AccountName = "n0", IdentityTokens = id1
                };
                p.AccountsSettings.Add(acct1);

                var id2   = new Identity(ukLocale);
                var acct2 = new Account("a1")
                {
                    AccountName = "n1", IdentityTokens = id2
                };

                p.AccountsSettings.Add(acct2);
            }

            // update identity on existing file
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                p.AccountsSettings.Accounts[0]
                .IdentityTokens
                .Update(new AccessToken("Atna|_NEW_", DateTime.Now.AddDays(1)));
            }

            // re-load file. ensure both accounts still exist
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                p.AccountsSettings.Accounts.Count.Should().Be(2);

                var acct0 = p.AccountsSettings.Accounts[0];
                // new
                acct0.IdentityTokens.ExistingAccessToken.TokenValue.Should().Be("Atna|_NEW_");

                // still here
                acct0.AccountName.Should().Be("n0");
                acct0.Locale.CountryCode.Should().Be("us");
                var acct1 = p.AccountsSettings.Accounts[1];
                acct1.AccountName.Should().Be("n1");
                acct1.Locale.CountryCode.Should().Be("uk");
            }
        }
Exemplo n.º 7
0
        public void replace_identity()
        {
            // create initial file
            using (var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile)) { }

            // load file. create 2 accounts
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                var id1   = new Identity(usLocale);
                var acct1 = new Account("a0")
                {
                    AccountName = "n0", IdentityTokens = id1
                };
                p.AccountsSettings.Add(acct1);

                var id2   = new Identity(ukLocale);
                var acct2 = new Account("a1")
                {
                    AccountName = "n1", IdentityTokens = id2
                };

                p.AccountsSettings.Add(acct2);
            }

            // update identity on existing file
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                var id = new Identity(ukLocale);

                var acct0 = p.AccountsSettings.Accounts[0];
                acct0.IdentityTokens = id;
            }

            // re-load file. ensure both accounts still exist
            using (var p = new AccountsSettingsPersister(TestFile))
            {
                p.AccountsSettings.Accounts.Count.Should().Be(2);

                var acct0 = p.AccountsSettings.Accounts[0];
                // new
                acct0.Locale.CountryCode.Should().Be("uk");

                // still here
                acct0.AccountName.Should().Be("n0");
                var acct1 = p.AccountsSettings.Accounts[1];
                acct1.AccountName.Should().Be("n1");
                acct1.Locale.CountryCode.Should().Be("uk");
            }
        }
Exemplo n.º 8
0
        public void atomic_update_at_end()
        {
            var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile);

            p.BeginTransation();

            // upserted account will not persist until CommitTransation
            var acct = p.AccountsSettings.Upsert("cng", "us");

            acct.AccountName = "foo";

            File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);
            p.IsInTransaction.Should().BeTrue();

            p.CommitTransation();
            p.IsInTransaction.Should().BeFalse();
        }
Exemplo n.º 9
0
        public void atomic_update_at_end()
        {
            var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile);

            p.BeginTransation();

            // upserted account will not persist until CommitTransation
            var acct = p.AccountsSettings.Upsert("cng", "us");

            acct.AccountName = "foo";

            File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);
            p.IsInTransaction.Should().BeTrue();

            p.CommitTransation();
            p.IsInTransaction.Should().BeFalse();


            var jsonOut = File.ReadAllText(TestFile);//.Should().Be(EMPTY_FILE);

            jsonOut.Should().Be(@"
{
  ""Accounts"": [
    {
      ""AccountId"": ""cng"",
      ""AccountName"": ""foo"",
      ""LibraryScan"": true,
      ""DecryptKey"": """",
      ""IdentityTokens"": {
        ""LocaleName"": ""us"",
        ""ExistingAccessToken"": {
          ""TokenValue"": ""Atna|"",
          ""Expires"": ""0001-01-01T00:00:00""
        },
        ""PrivateKey"": null,
        ""AdpToken"": null,
        ""RefreshToken"": null,
        ""Cookies"": []
      }
    }
  ]
}
".Trim());
        }
Exemplo n.º 10
0
        public void abandoned_transaction()
        {
            var p = new AccountsSettingsPersister(new AccountsSettings(), TestFile);

            try
            {
                p.BeginTransation();

                var acct = p.AccountsSettings.Upsert("cng", "us");
                acct.AccountName = "foo";
                throw new Exception();
            }
            catch { }
            finally
            {
                File.ReadAllText(TestFile).Should().Be(EMPTY_FILE);
                p.IsInTransaction.Should().BeTrue();
            }
        }
Exemplo n.º 11
0
        public void save_with_identity()
        {
            var id     = new Identity(usLocale);
            var idJson = JsonConvert.SerializeObject(id, Identity.GetJsonSerializerSettings());

            var accountsSettings = new AccountsSettings();

            accountsSettings.Add(new Account("a0")
            {
                AccountName = "n0", IdentityTokens = id
            });

            // dispose to cease auto-updates
            using (var p = new AccountsSettingsPersister(accountsSettings, TestFile)) { }

            var persister = new AccountsSettingsPersister(TestFile);
            var acct      = persister.AccountsSettings.Accounts[0];

            acct.AccountName.Should().Be("n0");
            acct.Locale.CountryCode.Should().Be("us");
        }
Exemplo n.º 12
0
        public void save_multiple_children()
        {
            var accountsSettings = new AccountsSettings();

            accountsSettings.Add(new Account("a0")
            {
                AccountName = "n0"
            });
            accountsSettings.Add(new Account("a1")
            {
                AccountName = "n1"
            });

            // dispose to cease auto-updates
            using (var p = new AccountsSettingsPersister(accountsSettings, TestFile)) { }

            var persister = new AccountsSettingsPersister(TestFile);

            persister.AccountsSettings.Accounts.Count.Should().Be(2);
            persister.AccountsSettings.Accounts[1].AccountName.Should().Be("n1");
        }