public void VerifyDeleteCredentialForOAuthReadsTwiceDeletesTwice()
        {
            var credentialStore = new MockCredentialStore();

            // add a stored basic auth credential to delete.
            credentialStore.Credentials.Add("https://example.com/", new Credential("john", "a1b2c3"));
            credentialStore.Credentials.Add("https://example.com/refresh_token", new Credential("john", "d4e5f6"));

            var bbAuth = new Authentication(credentialStore, null, null);

            var targetUri = new TargetUri("https://example.com/");

            bbAuth.DeleteCredentials(targetUri);

            var readCalls = credentialStore.MethodCalls
                            .Where(mc => mc.Key.Equals("ReadCredentials"))
                            .SelectMany(mc => mc.Value);

            // 2 read calls, 1 for the basic uri and 1 for /refresh_token
            Assert.Equal(2, readCalls.Count());
            Assert.Contains(readCalls, rc => rc.Key[0].Equals("https://example.com/"));
            Assert.Contains(readCalls, rc => rc.Key[0].Equals("https://example.com/refresh_token"));

            var deleteCalls = credentialStore.MethodCalls
                              .Where(mc => mc.Key.Equals("DeleteCredentials"))
                              .SelectMany(mc => mc.Value);

            // 2 delete call, 1 for the basic uri, 1 for /refresh_token as there is one
            Assert.Equal(2, deleteCalls.Count());
            Assert.Contains(deleteCalls, rc => rc.Key[0].Equals("https://example.com/"));
            Assert.Contains(deleteCalls, rc => rc.Key[0].Equals("https://example.com/refresh_token"));
        }
예제 #2
0
        public void VerifyDeleteCredentialForBasicAuthReadsTwiceDeletesOnce()
        {
            var credentialStore = new MockCredentialStore();

            // add a stored basic auth credential to delete.
            credentialStore.Credentials.Add("https://example.com/", new Credential("john", "squire"));

            var bbAuth = new Authentication(credentialStore, null, null);

            var targetUri = new TargetUri("https://example.com/");

            bbAuth.DeleteCredentials(targetUri);

            var readCalls = credentialStore.MethodCalls
                            .Where(mc => mc.Key.Equals("ReadCredentials"))
                            .SelectMany(mc => mc.Value);

            // 2 read calls, 1 for the basic uri and 1 for /refresh_token
            Assert.AreEqual(2, readCalls.Count());
            Assert.IsTrue(readCalls.Any(rc => rc.Key[0].Equals("https://example.com/")));
            Assert.IsTrue(readCalls.Any(rc => rc.Key[0].Equals("https://example.com/refresh_token")));

            var deleteCalls = credentialStore.MethodCalls
                              .Where(mc => mc.Key.Equals("DeleteCredentials"))
                              .SelectMany(mc => mc.Value);

            // 1 delete call, 1 for the basic uri 0 for /refresh_token as there isn't one
            Assert.AreEqual(1, deleteCalls.Count());
            Assert.IsTrue(deleteCalls.Any(rc => rc.Key[0].Equals("https://example.com/")));
            Assert.IsFalse(deleteCalls.Any(rc => rc.Key[0].Equals("https://example.com/refresh_token")));
        }
예제 #3
0
        public void VerifyDeleteCredentialForBasicAuthReadsTwiceDeletesOnce()
        {
            var credentialStore = new MockCredentialStore();

            // Add a stored basic authentication credential to delete.
            credentialStore.Credentials.Add("https://example.com/", _validBasicAuthCredentials);

            var bbAuth = new Authentication(RuntimeContext.Default, credentialStore, null, null);

            var targetUri = new TargetUri("https://example.com/");

            bbAuth.DeleteCredentials(targetUri);

            var readCalls = credentialStore.MethodCalls
                            .Where(mc => mc.Key.Equals("ReadCredentials"))
                            .SelectMany(mc => mc.Value);

            // 2 read calls, 1 for the basic uri and 1 for /refresh_token
            Assert.Equal(2, readCalls.Count());
            Assert.Contains(readCalls, rc => rc.Key[0].Equals("https://example.com/"));
            Assert.Contains(readCalls, rc => rc.Key[0].Equals("https://example.com/refresh_token"));

            var deleteCalls = credentialStore.MethodCalls
                              .Where(mc => mc.Key.Equals("DeleteCredentials"))
                              .SelectMany(mc => mc.Value);

            // 1 delete call, 1 for the basic uri 0 for /refresh_token as there isn't one
            Assert.Single(deleteCalls);
            Assert.Contains(deleteCalls, rc => rc.Key[0].Equals("https://example.com/"));
            Assert.DoesNotContain(deleteCalls, rc => rc.Key[0].Equals("https://example.com/refresh_token"));
        }
예제 #4
0
        public async Task VerifySetCredentialStoresValidCredentials()
        {
            var targetUri       = new TargetUri("https://example.com");
            var credentialStore = new MockCredentialStore();
            var credentials     = new Credential("a", "b");
            var bbAuth          = new Authentication(RuntimeContext.Default, credentialStore, null, null);

            try
            {
                await bbAuth.SetCredentials(targetUri, credentials);

                var writeCalls = credentialStore.MethodCalls
                                 .Where(mc => mc.Key.Equals("WriteCredentials"))
                                 .SelectMany(mc => mc.Value)
                                 .Where(wc => wc.Key.Contains(targetUri.ToString()) &&
                                        wc.Key.Contains(credentials.Username) &&
                                        wc.Key.Contains(credentials.Password));

                Assert.Single(writeCalls);
            }
            catch (AggregateException exception)
            {
                exception = exception.Flatten();
                throw exception.InnerException;
            }
        }
예제 #5
0
        public void VerifyDeleteCredentialDoesNotDeleteForNullTargetUri()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                try
                {
                    Task.Run(async() =>
                    {
                        var credentialStore = new MockCredentialStore();
                        var bbAuth          = new Authentication(RuntimeContext.Default, credentialStore, null, null);

                        await bbAuth.DeleteCredentials(null);

                        var deleteCalls = credentialStore.MethodCalls
                                          .Where(mc => mc.Key.Equals("DeleteCredentials"))
                                          .SelectMany(mc => mc.Value);

                        Assert.Empty(deleteCalls);
                    }).Wait();
                }
                catch (AggregateException exception)
                {
                    exception = exception.Flatten();
                    throw exception.InnerException;
                }
            });
        }
예제 #6
0
        public void VerifyDeleteCredentialDoesNotDeleteForNullTargetUri()
        {
            var credentialStore = new MockCredentialStore();
            var bbAuth          = new Authentication(credentialStore, null, null);

            bbAuth.DeleteCredentials(null);
        }
예제 #7
0
        public void VerifySetCredentialDoesNotStoresForNullCredentials()
        {
            var targetUri       = new TargetUri("https://example.com");
            var credentialStore = new MockCredentialStore();
            var bbAuth          = new Authentication(credentialStore, null, null);

            bbAuth.SetCredentials(targetUri, null);
        }
예제 #8
0
        public void VerifySetCredentialDoesNotStoreForNullTargetUri()
        {
            var credentialStore = new MockCredentialStore();
            var credentials     = new Credential("a", "b");
            var bbAuth          = new Authentication(credentialStore, null, null);

            bbAuth.SetCredentials(null, credentials);
        }
예제 #9
0
        public void VerifySetCredentialDoesNotStoreForTooLongUsername()
        {
            var targetUri       = new TargetUri("https://example.com");
            var credentialStore = new MockCredentialStore();
            var credentials     = new Credential(new string('x', 2047 + 1), "b");
            var bbAuth          = new Authentication(credentialStore, null, null);

            bbAuth.SetCredentials(targetUri, credentials);
        }
        public void VerifySetCredentialDoesNotStoreForNullTargetUri()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var credentialStore = new MockCredentialStore();
                var credentials     = new Credential("a", "b");
                var bbAuth          = new Authentication(credentialStore, null, null);

                bbAuth.SetCredentials(null, credentials);
            });
        }
        public void VerifySetCredentialDoesNotStoresForNullCredentials()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var targetUri       = new TargetUri("https://example.com");
                var credentialStore = new MockCredentialStore();
                var bbAuth          = new Authentication(credentialStore, null, null);

                bbAuth.SetCredentials(targetUri, null);
            });
        }
        public void VerifySetCredentialDoesNotStoreForTooLongPassword()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                var targetUri       = new TargetUri("https://example.com");
                var credentialStore = new MockCredentialStore();
                var credentials     = new Credential("a", new string('x', 2047 + 1));
                var bbAuth          = new Authentication(credentialStore, null, null);

                bbAuth.SetCredentials(targetUri, credentials);
            });
        }
예제 #13
0
        public void VerifyDeleteCredentialDoesNotDeleteForNullTargetUri()
        {
            var credentialStore = new MockCredentialStore();
            var bbAuth          = new Authentication(credentialStore, null, null);

            bbAuth.DeleteCredentials(null);

            var deleteCalls = credentialStore.MethodCalls
                              .Where(mc => mc.Key.Equals("DeleteCredentials"))
                              .SelectMany(mc => mc.Value);

            Assert.AreEqual(deleteCalls.Count(), 0);
        }
        public void VerifyDeleteCredentialDoesNotDeleteForNullTargetUri()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                var credentialStore = new MockCredentialStore();
                var bbAuth          = new Authentication(credentialStore, null, null);

                bbAuth.DeleteCredentials(null);

                var deleteCalls = credentialStore.MethodCalls
                                  .Where(mc => mc.Key.Equals("DeleteCredentials"))
                                  .SelectMany(mc => mc.Value);

                Assert.Empty(deleteCalls);
            });
        }
        public void VerifySetCredentialStoresValidCredentials()
        {
            var targetUri       = new TargetUri("https://example.com");
            var credentialStore = new MockCredentialStore();
            var credentials     = new Credential("a", "b");
            var bbAuth          = new Authentication(credentialStore, null, null);

            bbAuth.SetCredentials(targetUri, credentials);

            var writeCalls = credentialStore.MethodCalls
                             .Where(mc => mc.Key.Equals("WriteCredentials"))
                             .SelectMany(mc => mc.Value)
                             .Where(wc => wc.Key.Contains(targetUri.ToString()) &&
                                    wc.Key.Contains(credentials.Username) &&
                                    wc.Key.Contains(credentials.Password));

            Assert.Single(writeCalls);
        }
        public void VerifyDeleteCredentialForBasicAuthReadsQuinceDeletesTwiceIfHostCredentialsExistAndShareUsername()
        {
            var credentialStore = new MockCredentialStore();

            // add a stored basic auth credential to delete.
            // per host credentials
            credentialStore.Credentials.Add("https://example.com/", new Credential("john", "squire"));
            // per user credentials
            credentialStore.Credentials.Add("https://[email protected]/", new Credential("john", "squire"));

            var bbAuth = new Authentication(credentialStore, null, null);

            var targetUri = new TargetUri("https://[email protected]/");

            bbAuth.DeleteCredentials(targetUri);

            var readCalls = credentialStore.MethodCalls
                            .Where(mc => mc.Key.Equals("ReadCredentials"))
                            .SelectMany(mc => mc.Value);

            // 5 read calls
            // 1 for the basic uri with username
            // 1 for /refresh_token with username
            // 1 for the basic uri to compare username
            // 1 for the basic uri without username
            // 1 for /refresh_token without username
            Assert.Equal(5, readCalls.Count());
            Assert.Equal(1, readCalls.Count(rc => rc.Key[0].Equals("https://[email protected]/")));
            Assert.Equal(1, readCalls.Count(rc => rc.Key[0].Equals("https://[email protected]/refresh_token")));
            Assert.Equal(2, readCalls.Count(rc => rc.Key[0].Equals("https://example.com/")));
            Assert.Equal(1, readCalls.Count(rc => rc.Key[0].Equals("https://example.com/refresh_token")));

            var deleteCalls = credentialStore.MethodCalls
                              .Where(mc => mc.Key.Equals("DeleteCredentials"))
                              .SelectMany(mc => mc.Value);

            // 2 delete calls
            // 1 for the basic uri with username
            // 1 for the basic uri without username
            Assert.Equal(2, deleteCalls.Count());
            Assert.Equal(1, deleteCalls.Count(rc => rc.Key[0].Equals("https://[email protected]/")));
            Assert.DoesNotContain(deleteCalls, rc => rc.Key[0].Equals("https://example.com/refresh_token"));
            Assert.Equal(1, deleteCalls.Count(rc => rc.Key[0].Equals("https://example.com/")));
        }
예제 #17
0
        public void VerifyDeleteCredentialForBasicAuthReadsThriceDeletesOnceIfHostCredentialsExistAndDoNotShareUsername()
        {
            var credentialStore = new MockCredentialStore();

            // add a stored basic auth credential to delete.
            // per host credentials
            credentialStore.Credentials.Add("https://example.com/", new Credential("ian", "brown"));
            // per user credentials
            credentialStore.Credentials.Add("https://[email protected]/", new Credential("john", "squire"));

            var bbAuth = new Authentication(credentialStore, null, null);

            var targetUri = new TargetUri("https://[email protected]/");

            bbAuth.DeleteCredentials(targetUri);

            var readCalls = credentialStore.MethodCalls
                            .Where(mc => mc.Key.Equals("ReadCredentials"))
                            .SelectMany(mc => mc.Value);

            // 5 read calls
            // 1 for the basic uri with username
            // 1 for /refresh_token with username
            // 1 for the basic uri to compare username
            Assert.AreEqual(3, readCalls.Count());
            Assert.AreEqual(1, readCalls.Count(rc => rc.Key[0].Equals("https://[email protected]/")));
            Assert.AreEqual(1, readCalls.Count(rc => rc.Key[0].Equals("https://[email protected]/refresh_token")));
            Assert.AreEqual(1, readCalls.Count(rc => rc.Key[0].Equals("https://example.com/")));

            var deleteCalls = credentialStore.MethodCalls
                              .Where(mc => mc.Key.Equals("DeleteCredentials"))
                              .SelectMany(mc => mc.Value);

            // 1 delete calls
            // 1 for the basic uri with username
            // DOES NOT delete the Host credentials because they are for a different username.
            Assert.AreEqual(1, deleteCalls.Count());
            Assert.AreEqual(1, deleteCalls.Count(rc => rc.Key[0].Equals("https://[email protected]/")));
            Assert.IsFalse(deleteCalls.Any(rc => rc.Key[0].Equals("https://example.com/refresh_token")));
            Assert.IsFalse(deleteCalls.Any(rc => rc.Key[0].Equals("https://example.com/")));
        }
        public void VerifySetCredentialDoesNotStoreForNullTargetUri()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                try
                {
                    Task.Run(async() =>
                    {
                        var credentialStore = new MockCredentialStore();
                        var credentials     = new Credential("a", "b");
                        var bbAuth          = new Authentication(RuntimeContext.Default, credentialStore, null, null);

                        await bbAuth.SetCredentials(null, credentials);
                    }).Wait();
                }
                catch (System.AggregateException exception)
                {
                    exception = exception.Flatten();
                    throw exception.InnerException;
                }
            });
        }
예제 #19
0
        public void VerifyGetPerUserTargetUriDoesNotDuplicateUsernameOnActualUri()
        {
            var credentialStore = new MockCredentialStore();
            var bbAuth          = new Authentication(RuntimeContext.Default, credentialStore, null, null);

            var targetUri = new TargetUri("https://[email protected]");
            var username  = "******";

            var resultUri = targetUri.GetPerUserTargetUri(username);

            Assert.Equal("/", resultUri.AbsolutePath);
            Assert.Equal("https://[email protected]/", resultUri.QueryUri.AbsoluteUri);
            Assert.Equal("example.com", resultUri.DnsSafeHost);
            Assert.Equal("example.com", resultUri.Host);
            Assert.True(resultUri.IsAbsoluteUri);
            Assert.True(resultUri.IsDefaultPort);
            Assert.Equal(443, resultUri.Port);
            Assert.Null(resultUri.ProxyUri);
            Assert.Equal("https://[email protected]/", resultUri.QueryUri.AbsoluteUri);
            Assert.Equal("https", resultUri.Scheme);
            Assert.Equal("https://example.com/", resultUri.ToString(false, true, true));
        }
예제 #20
0
        public void VerifySetCredentialDoesNotStoresForNullCredentials()
        {
            Assert.Throws <ArgumentNullException>(() =>
            {
                try
                {
                    Task.Run(async() =>
                    {
                        var targetUri       = new TargetUri("https://example.com");
                        var credentialStore = new MockCredentialStore();
                        var bbAuth          = new Authentication(RuntimeContext.Default, credentialStore, null, null);

                        await bbAuth.SetCredentials(targetUri, null);
                    }).Wait();
                }
                catch (AggregateException exception)
                {
                    exception = exception.Flatten();
                    throw exception.InnerException;
                }
            });
        }
예제 #21
0
        public void VerifySetCredentialDoesNotStoreForTooLongUsername()
        {
            Assert.Throws <ArgumentOutOfRangeException>(() =>
            {
                try
                {
                    Task.Run(async() =>
                    {
                        var targetUri       = new TargetUri("https://example.com");
                        var credentialStore = new MockCredentialStore();
                        var credentials     = new Credential(new string('x', 2047 + 1), "b");
                        var bbAuth          = new Authentication(RuntimeContext.Default, credentialStore, null, null);

                        await bbAuth.SetCredentials(targetUri, credentials);
                    }).Wait();
                }
                catch (AggregateException exception)
                {
                    exception = exception.Flatten();
                    throw exception.InnerException;
                }
            });
        }
        public void VerifyGetPerUserTargetUriFormatsEmailUsernames()
        {
            var credentialStore = new MockCredentialStore();
            var bbAuth          = new Authentication(credentialStore, null, null);

            var targetUri = new TargetUri("https://example.com");
            var username  = "******";

            var resultUri = targetUri.GetPerUserTargetUri(username);

            Assert.Equal("/", resultUri.AbsolutePath);
            Assert.Equal("https://johnsquire%[email protected]/", resultUri.ActualUri.AbsoluteUri);
            Assert.Equal("example.com", resultUri.DnsSafeHost);
            Assert.Equal("example.com", resultUri.Host);
            Assert.True(resultUri.IsAbsoluteUri);
            Assert.True(resultUri.IsDefaultPort);
            Assert.Equal(443, resultUri.Port);
            Assert.Null(resultUri.ProxyUri);
            Assert.Equal("https://johnsquire%[email protected]/", resultUri.QueryUri.AbsoluteUri);
            Assert.Equal("https", resultUri.Scheme);
            Assert.Equal(new WebProxy().Address, resultUri.WebProxy.Address);
            Assert.Equal("https://example.com/", resultUri.ToString());
        }
예제 #23
0
        public void VerifyGetPerUserTargetUriDoesNotDuplicateUsernameOnActualUri()
        {
            var credentialStore = new MockCredentialStore();
            var bbAuth          = new Authentication(credentialStore, null, null);

            var targetUri = new TargetUri("https://[email protected]");
            var username  = "******";

            var resultUri = targetUri.GetPerUserTargetUri(username);

            Assert.AreEqual("/", resultUri.AbsolutePath);
            Assert.AreEqual("https://[email protected]/", resultUri.ActualUri.AbsoluteUri);
            Assert.AreEqual("example.com", resultUri.DnsSafeHost);
            Assert.AreEqual("example.com", resultUri.Host);
            Assert.AreEqual(true, resultUri.IsAbsoluteUri);
            Assert.AreEqual(true, resultUri.IsDefaultPort);
            Assert.AreEqual(443, resultUri.Port);
            Assert.AreEqual(null, resultUri.ProxyUri);
            Assert.AreEqual("https://[email protected]/", resultUri.QueryUri.AbsoluteUri);
            Assert.AreEqual("https", resultUri.Scheme);
            Assert.AreEqual(new WebProxy().Address, resultUri.WebProxy.Address);
            Assert.AreEqual("https://example.com/", resultUri.ToString());
        }