public void FileWatcherTestMethod() { FileWatcher watcher = ArrangeFileWatcher(); Thread.Sleep(4000); WriteNewLineToFile(); Thread.Sleep(4000); MasterLogger m_logger = CreateMasterLogger(); string consolidatedLogFilePath = @"C:\Windows\Temp\consolidated log\consolidatedLog.log"; FileStream fs = new FileStream(consolidatedLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sReader = new StreamReader(fs, Encoding.UTF8); string line; string lastLine = ""; while ((line = sReader.ReadLine()) != null) { lastLine = line; } string expectedJsonString = JsonConvert.SerializeObject(m_logger); watcher.Finish(); Assert.AreEqual(lastLine, expectedJsonString); }
public async Task <ApiServiceResponse <CompoPackClientUpdateModel> > CreateCompoPack(CompoPackClientUpdateModel cpcm) { //generate a new compopack on the database var newcompo = new CompoPack() { Id = Guid.NewGuid().ToString(), CompetitionID = cpcm.CompetitionId, CompoPackLink = cpcm.CompoPackLink, Instructions = cpcm.Instructions, isReleased = cpcm.isReleased }; try { db.CompoPacks.Add(newcompo); } catch (Exception ex) { MasterLogger.LogIssue(ex.Message, typeof(CompoPackController).Name, DateTime.Now, LogSeverity.EXCPETION); return(new ApiServiceResponse <CompoPackClientUpdateModel> { ResponseCode = ApiServiceResponseCode.FAILURE, ResponseObject = null, Message = ex.Message, }); } return(new ApiServiceResponse <CompoPackClientUpdateModel> { ResponseCode = ApiServiceResponseCode.SUCCESS, ResponseObject = null, Message = "" }); }
private async Task <ApiServiceResponse <User> > GetUserById(string id) { User user = db.Users.Find(id); if (user != null) { return(new ApiServiceResponse <User> { ResponseCode = ApiServiceResponseCode.SUCCESS, ResponseObject = user, Message = null }); } else { string message = "Failed to retrieve user with id:" + id; MasterLogger.LogIssue(message, typeof(UserApiController).Name, DateTime.Now, LogSeverity.ISSUE); return(new ApiServiceResponse <User> { ResponseCode = ApiServiceResponseCode.SUCCESS, ResponseObject = null, Message = message }); } }
public async Task <ApiServiceResponse <UserProfileFullModel> > UpdateUserInformation([FromBody] UserUpdatePartialModel user) { //TODO: HARDEN NON-PROFILETEXT var userClaimId = (await manager.FindByNameAsync(User.Identity.Name)).Id; var attemptingUser = db.Users.FirstOrDefault(u => u.UserID.Equals(userClaimId)); if (attemptingUser.Id != user.ID && !(await manager.IsInRoleAsync(userClaimId, "ADMIN"))) { //this user is attempting to edit another user, and is not an authorized administrator //log the occurence MasterLogger.LogIssue("Illegal user profile update. Attempter: " + attemptingUser.Id + "/" + attemptingUser.UserHandle + " Profile Attempted: " + user.ID, typeof(UserApiController).Name, DateTime.Now, LogSeverity.SECURITYWARNING); //fail silently return(new ApiServiceResponse <UserProfileFullModel> { ResponseCode = ApiServiceResponseCode.FAILURE, ResponseObject = null, Message = "ILLEGAL ACTION DETECTED" }); } //update user var oldUser = db.Users.Find(user.ID); if (oldUser == null) { string message = "Failed to retrieve user with id:" + oldUser.Id; MasterLogger.LogIssue(message, typeof(UserApiController).Name, DateTime.Now, LogSeverity.ISSUE); return(new ApiServiceResponse <UserProfileFullModel> { ResponseCode = ApiServiceResponseCode.FAILURE, ResponseObject = null, Message = message }); } //update user values oldUser.ProfileText = user.ProfileText; oldUser.UserHandle = user.UserHandle; db.SaveChanges(); var usermodel = new UserProfileFullModel { ID = oldUser.Id, ProfileText = oldUser.ProfileText, VaporAmount = oldUser.VaporAmount, AetherAmount = oldUser.AetherAmount, UserHandle = oldUser.UserHandle, Avatar = oldUser.Avatar }; return(new ApiServiceResponse <UserProfileFullModel> { ResponseCode = ApiServiceResponseCode.SUCCESS, ResponseObject = usermodel, Message = null }); }
public void DataWatcherTest() { //start a new databaseWatcher DatabaseWatcher dataWatcher = ArrangeDatabaseWatcher(); //expect serviceBroker: on bool serviceBrokerOn = false; SqlConnection sqlConnection = CreateSqlConnection(); SqlCommand sqlCommand = sqlConnection.CreateCommand(); sqlCommand.CommandText = $"SELECT is_broker_enabled FROM sys.databases WHERE name = '{_databaseName}';"; using (var reader = sqlCommand.ExecuteReader()) { while (reader.Read()) { if (reader["is_broker_enabled"].ToString() == "True") { serviceBrokerOn = true; } } } string newDocumentId = GetInsertedDocumentID(sqlConnection); //insert a new value and get its id from the database MasterLogger m_logger = CreateMasterLogger(sqlConnection, newDocumentId); string consolidatedLogFilePath = @"C:\Windows\Temp\consolidated log\consolidatedLog.log"; Thread.Sleep(4000); FileStream fs = new FileStream(consolidatedLogFilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); StreamReader sReader = new StreamReader(fs, Encoding.UTF8); string line; string lastLine = ""; while ((line = sReader.ReadLine()) != null) { lastLine = line; } string expectedJsonString = JsonConvert.SerializeObject(m_logger); bool newValueIsOnFile = lastLine.Equals(expectedJsonString); CloseSqlConnection(sqlConnection); bool hasAllExpectedValue = serviceBrokerOn && newValueIsOnFile ? true : false; dataWatcher.Finish(); Assert.AreEqual(true, hasAllExpectedValue); }
public async Task <ApiServiceResponse <ClientCompetitionModel> > CreateNewCompetition(ClientCompetitionUpdateModel updateModel) { var sanitizer = new HtmlSanitizer(); //create the competition object var competition = new Competition() { Id = Guid.NewGuid().ToString(), CurrentPhase = (int)CompetitionPhase.NOTRELEASED, CompoLength = updateModel.CompoLength, CompoStartDate = updateModel.CompoStartDate, HostUserID = updateModel.HostUserID, VoteLength = updateModel.CompoStartDate, Created = DateTime.Now, Modified = DateTime.Now, Title = sanitizer.Sanitize(updateModel.Title), CompoType = (int)updateModel.CompetitionType }; try { db.Competitions.Add(competition); db.SaveChanges(); //schedule competition in QuartzNet here //schedule competition in QuartzNet here } catch (Exception ex) { MasterLogger.LogIssue(ex.Message, this.GetType().Name, DateTime.Now, LogSeverity.EXCPETION); return(new ApiServiceResponse <ClientCompetitionModel> { ResponseCode = ApiServiceResponseCode.FAILURE, ResponseObject = null, Message = ex.Message }); } return(new ApiServiceResponse <ClientCompetitionModel> { ResponseCode = ApiServiceResponseCode.FAILURE, ResponseObject = CreateCompetitionClientModel(competition), Message = null }); }
public async Task <ApiServiceResponse <CompoPackClientUpdateModel> > CreateCompoPack(CompoPackClientUpdateModel cpcm) { //prevent XSS attacks var sanitizer = new HtmlSanitizer(); //create a new compo pack var cpck = new CompoPack() { Id = Guid.NewGuid().ToString(), CompetitionID = cpcm.CompetitionId, CompoPackLink = sanitizer.Sanitize(cpcm.CompoPackLink), Instructions = sanitizer.Sanitize(cpcm.Instructions), isReleased = false, Created = DateTime.Now, Modified = DateTime.Now }; try { db.CompoPacks.Add(cpck); db.SaveChanges(); } catch (Exception ex) { MasterLogger.LogIssue(ex.Message, this.GetType().Name, DateTime.Now, LogSeverity.EXCPETION); return(new ApiServiceResponse <CompoPackClientUpdateModel> { ResponseCode = ApiServiceResponseCode.FAILURE, Message = ex.Message, ResponseObject = null }); } return(new ApiServiceResponse <CompoPackClientUpdateModel> { ResponseCode = ApiServiceResponseCode.SUCCESS, Message = null, ResponseObject = cpcm }); }