Exemplo n.º 1
0
        public void EqualsCheckForUserId()
        {
            // Arrange
            var notifier = new Mock <INotifier>();
            var data     = new TwitterAccountData
            {
                UserId   = 123,
                ImageUrl = "http://example.com/image.png"
            };
            var a = new ContextEntry(notifier.Object, data, null);
            var b = new ContextEntry(notifier.Object, data, null);
            var c = new ContextEntry(notifier.Object, new TwitterAccountData
            {
                UserId   = 111,
                ImageUrl = "http://example.com/image.png"
            }, null);

            // Act
            var ab = a.Equals(b);
            var ba = b.Equals(a);
            var ac = a.Equals(c);

            // ReSharper disable once SuspiciousTypeConversion.Global
            var type = a.Equals(string.Empty);

            // Assert
            Assert.IsTrue(ab);
            Assert.IsTrue(ba);
            Assert.IsFalse(ac);
            Assert.IsFalse(type);
        }
Exemplo n.º 2
0
        public void AddingExistingAccountDoesNothing()
        {
            // Arrange
            var context = new Mock <IContextEntry>();

            context.SetupGet(c => c.UserId).Returns(123);

            var columnList  = new Mock <IColumnDefinitionList>();
            var contextList = new Mock <ITwitterContextList>();

            contextList.SetupGet(c => c.Contexts).Returns(new[] { context.Object });
            contextList.Setup(c => c.AddContext(It.IsAny <TwitterAccountData>())).Verifiable();

            var data = new TwitterAccountData {
                UserId = 123
            };
            var authResult = new AuthorizeResult(data, new Mock <IAuthorizer>().Object);

            var auth = new Mock <ITwitterAuthorizer>();

            auth.Setup(c => c.Authorize(It.IsAny <Action <string> >(), It.IsAny <Func <string> >(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(authResult));

            var vm = new AccountsDialogViewModel(columnList.Object, contextList.Object, auth.Object);

            // Act
            vm.AddAccountCommand.Execute(null);

            // Assert
            contextList.Verify(c => c.AddContext(It.IsAny <TwitterAccountData>()), Times.Never());
        }
        public void DecryptionOnlyDecryptsTokens()
        {
            // Arrange
            const string name   = "name";
            const string url    = "url";
            string       token  = DpApi.Encrypt("the oauth token");
            string       secret = DpApi.Encrypt("the token secret");
            const ulong  userId = 123456;

            var data = new TwitterAccountData
            {
                AccountName      = name,
                ImageUrl         = url,
                IsDefault        = true,
                OAuthToken       = token,
                OAuthTokenSecret = secret,
                RequiresConfirm  = true,
                UserId           = userId
            };

            // Act
            data.Decrypt();

            // Assert
            Assert.AreEqual(name, data.AccountName);
            Assert.AreEqual(url, data.ImageUrl);
            Assert.AreEqual("the oauth token", data.OAuthToken);
            Assert.AreEqual("the token secret", data.OAuthTokenSecret);
            Assert.AreEqual(userId, data.UserId);
            Assert.IsTrue(data.IsDefault);
            Assert.IsTrue(data.RequiresConfirm);
        }
Exemplo n.º 4
0
        public void NewAccountCanBeAdded()
        {
            // Arrange
            string fileName = Path.GetTempFileName();
            var    notifier = new Mock <INotifier>();
            var    list     = new TwitterContextList(notifier.Object, fileName, new Serializer(), null);

            var toAdd = new TwitterAccountData
            {
                AccountName      = "TheName",
                ImageUrl         = "http://example.com/image.url",
                IsDefault        = true,
                OAuthToken       = "TheAuthToken",
                OAuthTokenSecret = "TheAuthSecret",
                RequiresConfirm  = true,
                UserId           = 12345
            };

            // Act
            list.AddContext(toAdd);

            // Assert
            var fileContent = File.ReadAllText(fileName);

            Assert.IsTrue(fileContent.Contains(toAdd.AccountName));
            Assert.IsTrue(fileContent.Contains(toAdd.ImageUrl));
            Assert.IsFalse(fileContent.Contains(toAdd.OAuthToken));
            Assert.IsFalse(fileContent.Contains(toAdd.OAuthTokenSecret));
            Assert.IsTrue(fileContent.Contains(toAdd.UserId.ToString()));
        }
        public void DecryptedActionEncryptsAfterExecution()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            // Act
            data.ExecuteDecryptedAction(d => { });

            // Assert
            Assert.AreNotEqual("token", data.OAuthToken);
            Assert.AreNotEqual("secret", data.OAuthTokenSecret);
        }
Exemplo n.º 6
0
        public void HashCodeIsBaedOnUserId()
        {
            // Arrange
            var notifier = new Mock <INotifier>();
            var data     = new TwitterAccountData
            {
                UserId   = 123,
                ImageUrl = "http://example.com/image.png"
            };
            var entry = new ContextEntry(notifier.Object, data, null);

            // Act
            var hash = entry.GetHashCode();

            // Assert
            Assert.AreEqual(entry.UserId.GetHashCode(), hash);
        }
        public void EncryptedFuncPassesThroughReturnValue()
        {
            // Arrange

            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            Func <TwitterAccountData, int> action = d => 42;

            // Act
            var value = data.ExecuteDecryptedAction(action);

            // Assert
            Assert.AreEqual(42, value);
        }
Exemplo n.º 8
0
        public void SettingConfirmationUpdatesContext()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                RequiresConfirm = false
            };

            var context = new Mock <IContextEntry>();

            context.SetupGet(c => c.Data).Returns(data);

            // Act
            var entry = new AccountEntry(context.Object)
            {
                RequiresConfirmation = true
            };

            // Assert
            Assert.IsNotNull(entry);
            Assert.IsTrue(data.RequiresConfirm);
        }
Exemplo n.º 9
0
        public async Task <AuthorizeResult> Authorize(Action <string> displayPinPageAction, Func <string> getPinAction,
                                                      CancellationToken?token = null)
        {
            var auth = new PinAuthorizer
            {
                CredentialStore = new InMemoryCredentialStore
                {
                    ConsumerKey    = Constants.Auth.ConsumerKey,
                    ConsumerSecret = Constants.Auth.ConsumerSecret
                },
                GoToTwitterAuthorization = displayPinPageAction,
                GetPin = getPinAction
            };

            try
            {
                await auth.AuthorizeAsync().ConfigureAwait(false);
            }
            catch (TwitterQueryException ex)
            {
                if (token?.IsCancellationRequested != true)
                {
                    LogTo.ErrorException("Failed to authorize user", ex);
                }

                return(null);
            }

            var accountData = new TwitterAccountData
            {
                OAuthToken       = auth.CredentialStore.OAuthToken,
                OAuthTokenSecret = auth.CredentialStore.OAuthTokenSecret,
                AccountName      = auth.CredentialStore.ScreenName,
                UserId           = auth.CredentialStore.UserID
            };

            return(new AuthorizeResult(accountData, auth));
        }
Exemplo n.º 10
0
        public void EncryptedActionDecryptsBeforeExecution()
        {
            // Arrange
            var data = new TwitterAccountData
            {
                OAuthToken       = DpApi.Encrypt("token"),
                OAuthTokenSecret = DpApi.Encrypt("secret")
            };

            string decryptedToken              = "";
            string decryptedSecret             = "";
            Action <TwitterAccountData> action = d =>
            {
                decryptedToken  = d.OAuthToken;
                decryptedSecret = d.OAuthTokenSecret;
            };

            // Act
            data.ExecuteDecryptedAction(action);

            // Assert
            Assert.AreEqual("token", decryptedToken);
            Assert.AreEqual("secret", decryptedSecret);
        }
Exemplo n.º 11
0
 public AuthorizeResult(TwitterAccountData data, IAuthorizer auth)
 {
     Data = data;
     Auth = auth;
 }