public async Task <IActionResult> GetUserSession([HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = "assessment/session/{sessionId}")] HttpRequest request, string sessionId)
        {
            request.LogRequestHeaders(logService);

            var correlationId = Guid.Parse(correlationIdProvider.CorrelationId);

            logService.LogMessage($"CorrelationId: {correlationId} - Creating a new assessment", SeverityLevel.Information);

            var partitionKey        = sessionClient.GeneratePartitionKey(sessionId);
            var existingUserSession = await userSessionRepository.GetByIdAsync(sessionId, partitionKey).ConfigureAwait(false);

            if (existingUserSession is null)
            {
                logService.LogMessage($"Unable to find User Session with id {sessionId}.", SeverityLevel.Information);
                return(responseWithCorrelation.ResponseWithCorrelationId(HttpStatusCode.NoContent, correlationId));
            }

            var dfcUserSession = new DfcUserSession
            {
                SessionId    = existingUserSession.UserSessionId,
                CreatedDate  = existingUserSession.StartedDt,
                PartitionKey = existingUserSession.PartitionKey,
                Salt         = existingUserSession.Salt,
            };

            return(responseWithCorrelation.ResponseObjectWithCorrelationId(dfcUserSession, correlationId));
        }
Exemple #2
0
        public async Task CreateNewSkillsAssessmentCreatedSuccessfully()
        {
            // Arrange
            var dfcSession   = sessionClient.NewSession();
            var partitionKey = sessionClient.GeneratePartitionKey(dfcSession.SessionId);
            var client       = new RestClient(apiBaseUrl);
            var req          = new RestRequest("assessment/skills", Method.POST)
            {
                Body = new RequestBody("application/json", "integrationTest", JsonConvert.SerializeObject(dfcSession)),
            };

            // Act
            var response = client.Execute(req);

            // Assert
            Assert.True(response.IsSuccessful && response.StatusCode == HttpStatusCode.Created);

            var createdUserSession = await userSessionRepository.GetByIdAsync(dfcSession.SessionId, partitionKey).ConfigureAwait(false);

            Assert.NotNull(createdUserSession);
            Assert.True(createdUserSession.PartitionKey == dfcSession.PartitionKey &&
                        createdUserSession.Salt == dfcSession.Salt &&
                        createdUserSession.UserSessionId == dfcSession.SessionId &&
                        createdUserSession.StartedDt == dfcSession.CreatedDate);
        }
        public void GeneratePartitionKeyUsesGeneratePartitionKeyWhenCalled()
        {
            const string sessionId = "DummySessionId";

            sessionClient.GeneratePartitionKey(sessionId);
            A.CallTo(() => partitionKeyGenerator.GeneratePartitionKey(A <string> .Ignored, sessionId))
            .MustHaveHappened();
        }
        public async Task <UserSession> Reload(string sessionId)
        {
            var partitionKey = _sessionClient.GeneratePartitionKey(sessionId);
            var result       = await _cosmosService.ReadItemAsync(sessionId, partitionKey, CosmosCollection.Session);

            if (!result.IsSuccessStatusCode)
            {
                return(null);
            }

            var userSession =
                JsonConvert.DeserializeObject <UserSession>(await result.Content.ReadAsStringAsync());
            var dfcUserSession = new DfcUserSession()
            {
                Salt         = userSession.Salt,
                PartitionKey = userSession.PartitionKey,
                SessionId    = userSession.UserSessionId,
                Origin       = Origin.MatchSkills,
                CreatedDate  = userSession.SessionCreatedDate
            };

            _sessionClient.CreateCookie(dfcUserSession, false);
            return(userSession);
        }
        public async Task ReturnsMappedDfcUserSessionWhenUserSessionExists()
        {
            // Arrange
            const string userSessionId   = "userSessionId";
            const string partitionKey    = "partitionKey";
            const string salt            = "salt";
            var          startedDateTime = DateTime.UtcNow;

            var userSession = new UserSession
            {
                UserSessionId = userSessionId,
                PartitionKey  = partitionKey,
                Salt          = salt,
                StartedDt     = startedDateTime,
            };

            var expectedDfcUserSession = new DfcUserSession
            {
                Salt         = salt,
                SessionId    = userSessionId,
                CreatedDate  = startedDateTime,
                PartitionKey = partitionKey,
            };

            A.CallTo(() => userSessionRepository.GetByIdAsync(A <string> .Ignored, A <string> .Ignored)).Returns(userSession);
            A.CallTo(() => sessionClient.GeneratePartitionKey(A <string> .Ignored)).Returns(partitionKey);

            // Act
            var result = await functionApp.GetUserSession(httpRequest, "DummySessionId").ConfigureAwait(false);

            // Assert
            var okObjectResult     = Assert.IsType <OkObjectResult>(result);
            var deserialisedResult = JsonConvert.DeserializeObject <DfcUserSession>(okObjectResult.Value.ToString());

            deserialisedResult.Should().BeEquivalentTo(expectedDfcUserSession);
        }