Exemplo n.º 1
0
        public void NewSessionReturnsDfcUserSessionWithValuesFromConfig()
        {
            // Act
            var result = sessionClient.NewSession();

            // Assert
            Assert.StartsWith(sessionConfig.Salt, result.Salt, StringComparison.OrdinalIgnoreCase);
            Assert.StartsWith(sessionConfig.ApplicationName, result.PartitionKey, StringComparison.OrdinalIgnoreCase);
        }
Exemplo n.º 2
0
            public void Init()
            {
                _cosmosSettings = Options.Create(new CosmosSettings()
                {
                    ApiUrl                 = "https://test-account-not-real.documents.azure.com:443/",
                    ApiKey                 = "VGhpcyBpcyBteSB0ZXN0",
                    DatabaseName           = "DatabaseName",
                    UserSessionsCollection = "UserSessions"
                });
                var dummySession = new DfcUserSession()
                {
                    SessionId = "partitionkey-sessionid"
                };

                _client        = Substitute.For <CosmosClient>();
                _cosmosService = Substitute.For <ICosmosService>();
                _sessionClient = Substitute.For <ISessionClient>();
                _sessionConfig = Options.Create(new SessionConfig()
                {
                    Salt            = "ThisIsASalt",
                    ApplicationName = "matchskills"
                });

                _sessionClient.NewSession().Returns(dummySession);
            }
Exemplo n.º 3
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 async Task <string> CreateUserSession(CreateSessionRequest request)
        {
            if (request == null)
            {
                request = new CreateSessionRequest();
            }

            //Create new Session here
            var dfcUserSession = _sessionClient.NewSession();

            dfcUserSession.Origin = Origin.MatchSkills;
            _sessionClient.CreateCookie(dfcUserSession, true);

            var userSession = new UserSession()
            {
                UserSessionId       = dfcUserSession.SessionId,
                PartitionKey        = dfcUserSession.PartitionKey,
                Salt                = dfcUserSession.Salt,
                CurrentPage         = request.CurrentPage,
                PreviousPage        = request.PreviousPage,
                UserHasWorkedBefore = request.UserHasWorkedBefore,
                RouteIncludesDysac  = request.RouteIncludesDysac,
                LastUpdatedUtc      = DateTime.UtcNow,
                SessionCreatedDate  = dfcUserSession.CreatedDate
            };

            var result = await _cosmosService.CreateItemAsync(userSession, CosmosCollection.Session);

            return(result.IsSuccessStatusCode ? userSession.PrimaryKey : null);
        }
        public void NewSessionReturnsSessionObject()
        {
            // Arrange
            const string dummyPartitionKey = "dummyPartitionKey";
            var          dummySession      = new SessionResult
            {
                Counter          = 123,
                EncodedSessionId = "EncodedSessionId",
            };

            A.CallTo(() => sessionIdGenerator.CreateSession(A <string> .Ignored, A <DateTime> .Ignored)).Returns(dummySession);
            A.CallTo(() => partitionKeyGenerator.GeneratePartitionKey(A <string> .Ignored, A <string> .Ignored)).Returns(dummyPartitionKey);

            // Act
            var session = sessionClient.NewSession();

            // Assert
            Assert.NotNull(session);
            Assert.Equal($"{config.Salt}|{dummySession.Counter}", session.Salt);
            Assert.Equal(dummySession.EncodedSessionId, session.SessionId);
            Assert.Equal(dummyPartitionKey, session.PartitionKey);
        }
        public async Task ReturnsCreatedDfcUserSession()
        {
            // Arrange
            var dfcSession = new DfcUserSession
            {
                PartitionKey = "partitionKey",
                CreatedDate  = DateTime.UtcNow,
                SessionId    = "sessionId",
                Salt         = "ncs",
            };

            A.CallTo(() => sessionClient.NewSession()).Returns(dfcSession);

            var questionSet = new QuestionSet
            {
                QuestionSetVersion = "qsVersion",
                AssessmentType     = "short",
                MaxQuestions       = 5,
                PartitionKey       = "partitionKey",
                Description        = "short description",
                IsCurrent          = true,
                LastUpdated        = DateTimeOffset.UtcNow,
                QuestionSetKey     = "qsKey",
                Title   = "qstitle",
                Version = 1,
            };

            A.CallTo(() => questionSetRepository.GetCurrentQuestionSet(A <string> .Ignored)).Returns(questionSet);

            // Act
            var result = await functionApp.CreateNewShortAssessment(httpRequest).ConfigureAwait(false);

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

            // Assert
            deserialisedResult.Should().BeEquivalentTo(dfcSession);
            A.CallTo(() => userSessionRepository.CreateUserSession(A <UserSession> .Ignored)).MustHaveHappenedOnceExactly();
        }
        public async Task <IActionResult> CreateNewShortAssessment([HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "assessment/short")] HttpRequest request)
        {
            request.LogRequestHeaders(logService);

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

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

            var currentQuestionSetInfo = await questionSetRepository.GetCurrentQuestionSet(ShortAssessmentType).ConfigureAwait(false);

            if (currentQuestionSetInfo == null)
            {
                logService.LogMessage($"CorrelationId: {correlationId} - Unable to load latest question set {ShortAssessmentType}", SeverityLevel.Warning);
                return(responseWithCorrelation.ResponseWithCorrelationId(HttpStatusCode.NoContent, correlationId));
            }

            var dfcUserSession = sessionClient.NewSession();

            await CreateUserSession(currentQuestionSetInfo, dfcUserSession).ConfigureAwait(false);

            return(responseWithCorrelation.ResponseObjectWithCorrelationId(dfcUserSession, correlationId));
        }