Exemplo n.º 1
0
        public void GetAuthenticatedUserAsync_AuthUrlIsNotInvokedFirst_ThrowsInvalidOperationException()
        {
            // Setup
            var expectedUser = Substitute.For <IRtmUser>();

            const string fakeFrobUrl   = "My_Fake_Frob_Url";
            const string fakeFrob      = "My Fake Frob";
            const string fakeTokenUrl  = "My_Fake_Token_Url";
            var          fakeAuthToken = new GetTokenResponseData.AuthenticationTokenData {
                Token = "My Fake Token"
            };

            var fakeUrlFactory = Substitute.For <IAuthUrlFactory>();

            fakeUrlFactory.CreateGetFrobUrl().Returns(fakeFrobUrl);
            fakeUrlFactory.CreateGetTokenUrl(fakeFrob).Returns(fakeTokenUrl);

            var fakeApiClient = Substitute.For <IApiClient>();

            fakeApiClient.GetAsync <GetFrobResponseData>(fakeFrobUrl).Returns(Task.FromResult(new GetFrobResponseData {
                Frob = fakeFrob
            }));
            fakeApiClient.GetAsync <GetTokenResponseData>(fakeTokenUrl).Returns(Task.FromResult(new GetTokenResponseData {
                AuthenticationToken = fakeAuthToken
            }));

            var fakeUserFactory = Substitute.For <IUserFactory>();

            fakeUserFactory.CreateNewUser(fakeAuthToken).ReturnsForAnyArgs(expectedUser);

            // Execute
            var authenticator = GetItemUnderTest(fakeUrlFactory, fakeApiClient, fakeUserFactory);

            Assert.ThrowsAsync <InvalidOperationException>(() => authenticator.GetAutheticatedUserAsync());
        }
Exemplo n.º 2
0
        public void CreateNewUser_ValidAuthToken_InitsUserFromAuthTokenData()
        {
            // Setup
            var fakeAuthToken = new GetTokenResponseData.AuthenticationTokenData
            {
                User = new GetTokenResponseData.AuthenticationTokenData.UserInfo
                {
                    FullName = ExpectedFullName,
                    UserId   = ExpectedUserId,
                    UserName = ExpectedUserName
                },
                Permissions = ExpectedPermissionLevel,
                Token       = ExpectedToken
            };

            // Execute
            var factory = GetItemUnderTest();
            var actual  = factory.CreateNewUser(fakeAuthToken);

            // Verify
            Assert.AreEqual(ExpectedFullName, actual.FullName);
            Assert.AreEqual(ExpectedUserName, actual.UserName);
            Assert.AreEqual(ExpectedUserId, actual.UserId);
            Assert.AreEqual(ExpectedPermissionLevel, actual.Token.Permissions);
            Assert.AreEqual(ExpectedToken, actual.Token.Id);
        }
Exemplo n.º 3
0
        public IRtmUser CreateNewUser(GetTokenResponseData.AuthenticationTokenData authTokenData)
        {
            if (authTokenData == null)
            {
                throw new ArgumentNullException(nameof(authTokenData));
            }

            var authToken = new AuthenticationToken
            {
                Id          = authTokenData.Token,
                Permissions = authTokenData.Permissions
            };

            return(new RtmUser
            {
                UserId = authTokenData.User.UserId,
                UserName = authTokenData.User.UserName,
                FullName = authTokenData.User.FullName,
                Token = authToken
            });
        }
Exemplo n.º 4
0
        public async Task GetAuthenticatedUserAsync_AuthUrlIsInvokedFirst_GetsUser()
        {
            // Setup
            var expectedUser = Substitute.For <IRtmUser>();

            const string fakeFrobUrl       = "My_Fake_Frob_Url";
            const string fakeFrob          = "My Fake Frob";
            const string fakeTokenUrl      = "My_Fake_Token_Url";
            var          fakeAuthTokenData = new GetTokenResponseData.AuthenticationTokenData {
                Token = "My Fake Token"
            };
            var fakePermissionLevel = (PermissionLevel)int.MaxValue;

            var fakeUrlFactory = Substitute.For <IAuthUrlFactory>();

            fakeUrlFactory.CreateGetFrobUrl().Returns(fakeFrobUrl);
            fakeUrlFactory.CreateGetTokenUrl(fakeFrob).Returns(fakeTokenUrl);

            var fakeApiClient = Substitute.For <IApiClient>();

            fakeApiClient.GetAsync <GetFrobResponseData>(fakeFrobUrl).Returns(Task.FromResult(new GetFrobResponseData {
                Frob = fakeFrob
            }));
            fakeApiClient.GetAsync <GetTokenResponseData>(fakeTokenUrl).Returns(Task.FromResult(new GetTokenResponseData {
                AuthenticationToken = fakeAuthTokenData
            }));

            var fakeUserFactory = Substitute.For <IUserFactory>();

            fakeUserFactory.CreateNewUser(fakeAuthTokenData).ReturnsForAnyArgs(expectedUser);

            // Execute
            var authenticator = GetItemUnderTest(fakeUrlFactory, fakeApiClient, fakeUserFactory);
            await authenticator.GetAuthenticationUrlAsync(fakePermissionLevel).ConfigureAwait(false);

            var actual = await authenticator.GetAutheticatedUserAsync().ConfigureAwait(false);

            // Verify
            Assert.AreSame(expectedUser, actual);
        }