public void WhenGenerateSessionIdCalledWithSaltIsWhitespace_ValueShouldNotBeNull() { var sessionId = SessionIdHelper.GenerateSessionId("", DateTime.UtcNow); sessionId.Should().NotBeNullOrWhiteSpace(); sessionId.Should().NotBeNullOrEmpty(); }
public void WhenGenerateSessionIdCalledWithSaltAndWithoutDate_ValueShouldNotBeNull() { var sessionId = SessionIdHelper.GenerateSessionId("Salt"); sessionId.Should().NotBeNullOrWhiteSpace(); sessionId.Should().NotBeNullOrEmpty(); }
public void PartitionKey_WithVariousDates_ShouldMatchTheory(int year, int month, string expectedPartitionKey) { var creationDate = new DateTime(year, month, 1); var sessionId = SessionIdHelper.GenerateSessionId(SESSION_SALT, creationDate); var datetimeStamp = SessionIdHelper.Decode(SESSION_SALT, sessionId); string partitionKey = SessionIdHelper.GetYearMonth(datetimeStamp); Assert.Equal(expectedPartitionKey, partitionKey); }
public void PartitionKey_WithNewSession_ShouldDecodeToCurrentMonthYear() { var expected = DateTime.Now.ToString("yyyyMM"); var sessionId = SessionIdHelper.GenerateSessionId(SESSION_SALT); var datetimeStamp = SessionIdHelper.Decode(SESSION_SALT, sessionId); string partitionKey = SessionIdHelper.GetYearMonth(datetimeStamp); Assert.Equal(expected, partitionKey); }
public void EncodedValueShouldReturnSameValueWhenDecoded() { string Alphabet = "acefghjkmnrstwxyz23456789"; var salt = "BatteryHorseStapleCorrect"; var sessionId = SessionIdHelper.GenerateSessionId(salt, DateTime.UtcNow); var hashids = new Hashids(salt, 4, Alphabet); var digits = hashids.DecodeLong(sessionId).First(); var decode = SessionIdHelper.Decode(salt, sessionId); decode.Should().Be(digits.ToString()); }
private static UserSession CreateSession(string questionSetVersion, int maxQuestions, string languageCode = "en") { string partitionKey = DateTime.Now.ToString("yyyyMM"); string salt = Guid.NewGuid().ToString(); return(new UserSession() { UserSessionId = SessionIdHelper.GenerateSessionId(salt) + "_shl", Salt = salt, StartedDt = DateTime.Now, LanguageCode = languageCode, PartitionKey = partitionKey, AssessmentState = new AssessmentState(questionSetVersion, maxQuestions) }); }
public void GenerateSessionId_With1000_ShouldAllBeUnique() { int amount = 1000; var codes = new List <string>(); for (var i = 0; i < amount; i++) { var index = new Random().Next(0); var hash = SessionIdHelper.GenerateSessionId(SESSION_SALT); if (codes.Contains(hash) == true) { throw new Exception($"duplicate {i}"); } codes.Add(hash); } int maxLength = codes.Select(x => x.Length).OrderByDescending(x => x).First(); Output.WriteLine($"Code max length {maxLength}"); Assert.Equal(amount, codes.Count); }
public static async Task <HttpResponseMessage> Run( [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = "assessment")] HttpRequest req, ILogger log, [Inject] IHttpRequestHelper httpRequestHelper, [Inject] IHttpResponseMessageHelper httpResponseMessageHelper, [Inject] IUserSessionRepository userSessionRepository, [Inject] IQuestionSetRepository questionSetRepository, [Inject] IOptions <AppSettings> appSettings) { try { var correlationId = httpRequestHelper.GetDssCorrelationId(req); if (string.IsNullOrEmpty(correlationId)) { log.LogWarning("Unable to locate 'DssCorrelationId' in request header"); correlationId = Guid.NewGuid().ToString(); } log.LogInformation($"CorrelationId: {correlationId} - Creating a new assessment"); // Get the assessmentType and questionSetTitle values from the query string var queryDictionary = System.Web.HttpUtility.ParseQueryString(req.QueryString.ToString()); var assessmentType = queryDictionary.Get("assessmentType"); if (string.IsNullOrEmpty(assessmentType)) { log.LogInformation($"CorrelationId: {correlationId} - Missing assessmentType {assessmentType}"); return(httpResponseMessageHelper.BadRequest()); } // Get the current question set version for this assesssment type and title (supplied by CMS - configured in appsettings) var currentQuestionSetInfo = await questionSetRepository.GetCurrentQuestionSet(assessmentType); if (currentQuestionSetInfo == null) { log.LogInformation($"CorrelationId: {correlationId} - Unable to load latest question set {assessmentType}"); return(httpResponseMessageHelper.NoContent()); } // Create a new user session string salt = appSettings.Value.SessionSalt; string sessionId = SessionIdHelper.GenerateSessionId(salt); string partitionKey = PartitionKeyGenerator.UserSession(sessionId); var userSession = new UserSession() { UserSessionId = sessionId, Salt = salt, StartedDt = DateTime.Now, LanguageCode = "en", PartitionKey = partitionKey, AssessmentState = new AssessmentState(currentQuestionSetInfo.QuestionSetVersion, currentQuestionSetInfo.MaxQuestions), AssessmentType = currentQuestionSetInfo.AssessmentType.ToLower() }; await userSessionRepository.CreateUserSession(userSession); log.LogInformation($"CorrelationId: {correlationId} - Finished creating new assessment {userSession.UserSessionId}"); var result = new FilterSessionResponse() { SessionId = userSession.PrimaryKey }; return(httpResponseMessageHelper.Ok(JsonConvert.SerializeObject(result))); } catch (Exception ex) { log.LogError(ex, "Fatal exception {message}", ex.ToString()); return(new HttpResponseMessage { StatusCode = HttpStatusCode.InternalServerError }); } }