Beispiel #1
0
        public async Task ShouldHaveConflictStatusCodeIfAlreadyLinked(
            string code,
            string accessToken,
            string idToken,
            string discordToken,
            Models.User discordUser,
            Guid identityUserId,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            userService.GetDiscordUserInfo(Is(discordToken), Any <CancellationToken>()).Returns(discordUser);
            userService.GetUserIdFromIdentityToken(Is(idToken)).Returns(identityUserId);
            userService.ExchangeOAuth2CodeForToken(Is(code), Any <CancellationToken>()).Returns(discordToken);

            var exception = new ApiException(string.Empty, (int)HttpStatusCode.Conflict, string.Empty, null !, null !);

            userService.LinkDiscordIdToUser(Any <Snowflake>(), Any <Guid>(), Any <string>(), Any <CancellationToken>()).Throws(exception);
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            await controller.Callback(code);

            httpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.Conflict);
        }
Beispiel #2
0
        public async Task ShouldSetStatusToAlreadyLinkedOnTheModelViewIfThereWasAnApiExceptionWithStatus409(
            string code,
            string accessToken,
            string idToken,
            string discordToken,
            Models.User discordUser,
            Guid identityUserId,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            userService.GetDiscordUserInfo(Is(discordToken), Any <CancellationToken>()).Returns(discordUser);
            userService.GetUserIdFromIdentityToken(Is(idToken)).Returns(identityUserId);
            userService.ExchangeOAuth2CodeForToken(Is(code), Any <CancellationToken>()).Returns(discordToken);

            var exception = new ApiException(string.Empty, (int)HttpStatusCode.Conflict, string.Empty, null !, null !);

            userService.LinkDiscordIdToUser(Any <Snowflake>(), Any <Guid>(), Any <string>(), Any <CancellationToken>()).Throws(exception);
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            var result = await controller.Callback(code) as ViewResult;

            result.Should().NotBeNull();
            var model = (AccountLinkViewModel)(result !.Model !);

            model.Status.Should().Be(AccountLinkStatus.AlreadyLinked);
        }
Beispiel #3
0
        public async Task ShouldReturnBadRequestIfIdTokenCookieNotPresent(
            string code,
            string accessToken,
            [Substitute] HttpContext httpContext,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            httpContext.Request.Cookies.TryGetValue(Is(".Brighid.AccessToken"), out Any <string?>()).Returns(x =>
            {
                x[1] = accessToken;
                return(true);
            });

            httpContext.Request.Cookies.TryGetValue(Is(".Brighid.IdentityToken"), out Any <string?>()).Returns(x =>
            {
                x[1] = null;
                return(false);
            });
            SetupTempData(controller);

            var result = await controller.Callback(code);

            result.Should().BeOfType <BadRequestResult>();
        }
Beispiel #4
0
        public async Task ShouldHaveCreatedStatusCodeOnSuccess(
            string code,
            string accessToken,
            string idToken,
            string discordToken,
            Models.User discordUser,
            Guid identityUserId,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            userService.GetDiscordUserInfo(Is(discordToken), Any <CancellationToken>()).Returns(discordUser);
            userService.GetUserIdFromIdentityToken(Is(idToken)).Returns(identityUserId);
            userService.ExchangeOAuth2CodeForToken(Is(code), Any <CancellationToken>()).Returns(discordToken);
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            await controller.Callback(code);

            httpContext.Response.StatusCode.Should().Be((int)HttpStatusCode.Created);
        }
Beispiel #5
0
        public async Task ShouldSetStatusToSuccessOnTheModelView(
            string code,
            string accessToken,
            string idToken,
            string discordToken,
            Models.User discordUser,
            Guid identityUserId,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            userService.GetDiscordUserInfo(Is(discordToken), Any <CancellationToken>()).Returns(discordUser);
            userService.GetUserIdFromIdentityToken(Is(idToken)).Returns(identityUserId);
            userService.ExchangeOAuth2CodeForToken(Is(code), Any <CancellationToken>()).Returns(discordToken);
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            var result = await controller.Callback(code) as ViewResult;

            result.Should().NotBeNull();
            var model = (AccountLinkViewModel)(result !.Model !);

            model.Status.Should().Be(AccountLinkStatus.Success);
        }
Beispiel #6
0
        public async Task ShouldLinkTheDiscordIdToTheUser(
            string code,
            string accessToken,
            string idToken,
            string discordToken,
            Models.User discordUser,
            Guid identityUserId,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            userService.GetDiscordUserInfo(Is(discordToken), Any <CancellationToken>()).Returns(discordUser);
            userService.GetUserIdFromIdentityToken(Is(idToken)).Returns(identityUserId);
            userService.ExchangeOAuth2CodeForToken(Is(code), Any <CancellationToken>()).Returns(discordToken);
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            await controller.Callback(code);

            await userService.Received().LinkDiscordIdToUser(Is(discordUser.Id), Is(identityUserId), Is(accessToken), Is(httpContext.RequestAborted));
        }
Beispiel #7
0
        public async Task ShouldReturnBadRequestIfAccessTokenCookieNotPresent(
            string code,
            [Substitute] HttpContext httpContext,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            SetupTempData(controller);

            var result = await controller.Callback(code);

            result.Should().BeOfType <BadRequestResult>();
        }
Beispiel #8
0
        public async Task ShouldExchangeTheCodeForAToken(
            string code,
            string accessToken,
            string idToken,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            await controller.Callback(code);

            await userService.Received().ExchangeOAuth2CodeForToken(Is(code), Is(httpContext.RequestAborted));
        }
Beispiel #9
0
        public async Task ShouldGetTheIdentityUserIdFromTheIdToken(
            string code,
            string accessToken,
            string idToken,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            await controller.Callback(code);

            userService.Received().GetUserIdFromIdentityToken(Is(idToken));
        }
Beispiel #10
0
        public async Task ShouldUseTheTokenToFetchTheUsersDiscordId(
            string code,
            string accessToken,
            string idToken,
            string discordToken,
            [Substitute] HttpContext httpContext,
            [Frozen, Substitute] IUserService userService,
            [Target] OAuth2Controller controller
            )
        {
            controller.ControllerContext = new ControllerContext {
                HttpContext = httpContext
            };
            userService.ExchangeOAuth2CodeForToken(Is(code), Any <CancellationToken>()).Returns(discordToken);
            SetupHttpContext(httpContext, accessToken, idToken);
            SetupTempData(controller);

            await controller.Callback(code);

            await userService.Received().GetDiscordUserInfo(Is(discordToken), Is(httpContext.RequestAborted));
        }