Example #1
0
        public IActionResult LinkLearningHubSso([FromQuery] LinkLearningHubRequest linkLearningHubRequest)
        {
            if (!ModelState.IsValid)
            {
                throw new LearningHubLinkingRequestException("Invalid Learning Hub request.");
            }

            var learningHubResourcedId = learningHubLinkService.ValidateLinkingRequestAndExtractDestinationResourceId(
                linkLearningHubRequest,
                HttpContext.Session.GetString(LinkLearningHubRequest.SessionIdentifierKey)
                );

            var delegateId             = User.GetCandidateIdKnownNotNull();
            var isAccountAlreadyLinked = learningHubLinkService.IsLearningHubAccountLinked(delegateId);

            learningHubLinkService.LinkLearningHubAccountIfNotLinked(delegateId, linkLearningHubRequest.UserId);

            var model = new LinkLearningHubViewModel(isAccountAlreadyLinked, learningHubResourcedId);

            return(View("../LinkLearningHubSso", model));
        }
        ValidateLinkingRequestAndExtractDestinationResourceId_throws_exception_when_stored_sessionIdentifier_is_invalid()
        {
            // Given
            var linkLearningHubRequest = new LinkLearningHubRequest
            {
                Hash   = "valid-hash",
                State  = $"{Guid.NewGuid()}_refId:1234",
                UserId = 12345,
            };

            // When
            Action act = () =>
                         learningHubLinkService.ValidateLinkingRequestAndExtractDestinationResourceId(
                linkLearningHubRequest,
                "invalid-guid"
                );

            // Then
            act.Should().Throw <LearningHubLinkingRequestException>()
            .WithMessage("Invalid Learning Hub linking request session identifier.");
        }
        public void LinkLearningHubSso_invalid_state_throws_exception()
        {
            // Given
            controller.ModelState.AddModelError("Hash", "Required.");

            // When
            Action a = () => controller.LinkLearningHubSso(new LinkLearningHubRequest());

            // Then
            using (new AssertionScope())
            {
                a.Should().Throw <LearningHubLinkingRequestException>();
                A.CallTo(
                    () => learningHubLinkService.ValidateLinkingRequestAndExtractDestinationResourceId(
                        A <LinkLearningHubRequest> ._,
                        A <string> ._
                        )
                    )
                .MustNotHaveHappened();
                A.CallTo(() => learningHubLinkService.IsLearningHubAccountLinked(A <int> ._)).MustNotHaveHappened();
                A.CallTo(() => learningHubLinkService.LinkLearningHubAccountIfNotLinked(A <int> ._, A <int> ._))
                .MustNotHaveHappened();
            }
        }