Пример #1
0
        public void delete_updates()
        {
            var i = 0;

            void update(object sender, EventArgs e) => i++;

            var accountsSettings = new AccountsSettings();

            accountsSettings.Updated += update;

            accountsSettings.Accounts.Count.Should().Be(0);
            i.Should().Be(0);

            _ = accountsSettings.Upsert("cng", "us");
            accountsSettings.Accounts.Count.Should().Be(1);
            i.Should().Be(1);

            accountsSettings.Delete("baz", "baz").Should().BeFalse();
            accountsSettings.Accounts.Count.Should().Be(1);
            i.Should().Be(1);

            accountsSettings.Delete("cng", "us").Should().BeTrue();
            accountsSettings.Accounts.Count.Should().Be(0);
            i.Should().Be(2); // <== this is the one being tested
        }
Пример #2
0
 private void SaveButtonClickHandler(object sender, RoutedEventArgs e)
 {
     //TODO: try to connect connect with the inputed data to validate
     AccountsSettings.Save();
     ApplicationSettings.Save();
     Close();
 }
Пример #3
0
        private void persist(AccountsSettings accountsSettings)
        {
            var existingAccounts = accountsSettings.Accounts;
            var dtos             = getRowDtos();

            // editing account id is a special case. an account is defined by its account id, therefore this is really a different account. the user won't care about this distinction though.
            // these will be caught below by normal means and re-created minus the convenience of persisting identity tokens

            // delete
            for (var i = existingAccounts.Count - 1; i >= 0; i--)
            {
                var existing = existingAccounts[i];
                if (!dtos.Any(dto =>
                              dto.AccountId?.ToLower().Trim() == existing.AccountId.ToLower() &&
                              dto.LocaleName == existing.Locale?.Name))
                {
                    accountsSettings.Delete(existing);
                }
            }

            // upsert each. validation occurs through Account and AccountsSettings
            foreach (var dto in dtos)
            {
                var acct = accountsSettings.Upsert(dto.AccountId, dto.LocaleName);
                acct.LibraryScan = dto.LibraryScan;
                acct.AccountName
                    = string.IsNullOrWhiteSpace(dto.AccountName)
                                        ? $"{dto.AccountId} - {dto.LocaleName}"
                                        : dto.AccountName.Trim();
            }
        }
Пример #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);
        }
Пример #5
0
        public void upsert_new()
        {
            var accountsSettings = new AccountsSettings();

            accountsSettings.Accounts.Count.Should().Be(0);

            accountsSettings.Upsert("cng", "us");

            accountsSettings.Accounts.Count.Should().Be(1);
            accountsSettings.GetAccount("cng", "us").AccountId.Should().Be("cng");
        }
Пример #6
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);
        }
Пример #7
0
        public void upsert_exists()
        {
            var accountsSettings = new AccountsSettings();
            var orig             = accountsSettings.Upsert("cng", "us");

            orig.AccountName = "foo";

            var exists = accountsSettings.Upsert("cng", "us");

            exists.AccountName.Should().Be("foo");

            orig.Should().IsSameOrEqualTo(exists);
        }
Пример #8
0
        public void delete_account()
        {
            var accountsSettings = new AccountsSettings();
            var acct             = accountsSettings.Upsert("cng", "us");

            accountsSettings.Accounts.Count.Should().Be(1);

            var removed = accountsSettings.Delete(acct);

            removed.Should().BeTrue();

            accountsSettings.Accounts.Count.Should().Be(0);
        }
Пример #9
0
        public void delete_where()
        {
            var accountsSettings = new AccountsSettings();

            _ = accountsSettings.Upsert("cng", "us");
            accountsSettings.Accounts.Count.Should().Be(1);

            accountsSettings.Delete("baz", "baz").Should().BeFalse();
            accountsSettings.Accounts.Count.Should().Be(1);

            accountsSettings.Delete("cng", "us").Should().BeTrue();
            accountsSettings.Accounts.Count.Should().Be(0);
        }
Пример #10
0
        public void serialize()
        {
            var id               = JsonConvert.SerializeObject(Identity.Empty, Identity.GetJsonSerializerSettings());
            var jsonIn           = $@"
{{
  ""Accounts"": [
	{{
      ""AccountId"": ""cng"",
      ""AccountName"": ""my main login"",
      ""DecryptKey"": ""asdfasdf"",
      ""IdentityTokens"": {id}
    }}
  ]
}}
".Trim();
            var accountsSettings = AccountsSettings.FromJson(jsonIn);

            var jsonOut = accountsSettings.ToJson();

            jsonOut.Should().Be(@"
{
  ""Accounts"": [
    {
      ""AccountId"": ""cng"",
      ""AccountName"": ""my main login"",
      ""LibraryScan"": true,
      ""DecryptKey"": ""asdfasdf"",
      ""IdentityTokens"": {
        ""LocaleName"": ""[empty]"",
        ""ExistingAccessToken"": {
          ""TokenValue"": ""Atna|"",
          ""Expires"": ""0001-01-01T00:00:00""
        },
        ""PrivateKey"": null,
        ""AdpToken"": null,
        ""RefreshToken"": null,
        ""Cookies"": []
      }
    }
  ]
}
".Trim());
        }
Пример #11
0
        public void _1_account_new()
        {
            var json             = @"
{
  ""Accounts"": [
	{
      ""AccountId"": ""cng"",
      ""AccountName"": ""my main login"",
      ""DecryptKey"": ""asdfasdf"",
      ""IdentityTokens"": null
    }
  ]
}
".Trim();
            var accountsSettings = AccountsSettings.FromJson(json);

            accountsSettings.Accounts.Count.Should().Be(1);
            accountsSettings.Accounts[0].AccountId.Should().Be("cng");
            accountsSettings.Accounts[0].IdentityTokens.Should().BeNull();
        }
Пример #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");
        }
Пример #13
0
        public void violate_validation()
        {
            var accountsSettings = new AccountsSettings();

            var idIn = new Identity(usLocale);

            var a1 = new Account("a")
            {
                AccountName = "one", IdentityTokens = idIn
            };

            accountsSettings.Add(a1);

            var a2 = new Account("a")
            {
                AccountName = "two", IdentityTokens = idIn
            };

            // violation: validate()
            Assert.ThrowsException <InvalidOperationException>(() => accountsSettings.Add(a2));
        }
Пример #14
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");
        }
Пример #15
0
        public void get_where()
        {
            var idUs  = new Identity(usLocale);
            var acct1 = new Account("cng")
            {
                IdentityTokens = idUs, AccountName = "foo"
            };

            var idUk  = new Identity(ukLocale);
            var acct2 = new Account("cng")
            {
                IdentityTokens = idUk, AccountName = "bar"
            };

            var accountsSettings = new AccountsSettings();

            accountsSettings.Add(acct1);
            accountsSettings.Add(acct2);

            accountsSettings.GetAccount("cng", "uk").AccountName.Should().Be("bar");
        }
Пример #16
0
        public void _1_account_populated()
        {
            var id = GetIdentityJson(Future);

            var json             = $@"
{{
  ""Accounts"": [
	{{
      ""AccountId"": ""cng"",
      ""AccountName"": ""my main login"",
      ""DecryptKey"": ""asdfasdf"",
      ""IdentityTokens"": {id}
    }}
  ]
}}
".Trim();
            var accountsSettings = AccountsSettings.FromJson(json);

            accountsSettings.Accounts.Count.Should().Be(1);
            accountsSettings.Accounts[0].AccountId.Should().Be("cng");
            accountsSettings.Accounts[0].IdentityTokens.Should().NotBeNull();
            accountsSettings.Accounts[0].IdentityTokens.ExistingAccessToken.TokenValue.Should().Be(AccessTokenValue);
        }
Пример #17
0
        public void identity_violate_validation()
        {
            var accountsSettings = new AccountsSettings();

            var idIn = new Identity(usLocale);

            var a1 = new Account("a")
            {
                AccountName = "one", IdentityTokens = idIn
            };

            accountsSettings.Add(a1);

            var a2 = new Account("a")
            {
                AccountName = "two"
            };

            accountsSettings.Add(a2);

            // violation: GetAccount.SingleOrDefault
            Assert.ThrowsException <InvalidOperationException>(() => a2.IdentityTokens = idIn);
        }
Пример #18
0
        public void _0_accounts()
        {
            var accountsSettings = AccountsSettings.FromJson(EMPTY_FILE);

            accountsSettings.Accounts.Count.Should().Be(0);
        }