public void Save_OnFilePost_CreatesDirectoryAndFile() { int projectId = new Random().Next(1, 100); string fileName = Path.GetRandomFileName() + ".log"; string filePath = Path.Combine(AppContext.BaseDirectory, fileName); // setup _appSettings.LogFileProcessingDirectory.Returns(AppContext.BaseDirectory); var currentUser = new UserIdentity() { Id = Guid.NewGuid(), UserName = "******" }; currentUser.Claims = new string[] { Claims.ProjectEdit }; var browser = new Browser((bootstrapper) => bootstrapper.Module(new LogFileModule(_dbContext, _appSettings, _createLogFileCommand, _deleteLogFileCommand, _dirWrap)) .RequestStartup((container, pipelines, context) => { context.CurrentUser = currentUser; }) ); byte[] buffer = new byte[100]; new Random().NextBytes(buffer); using (MemoryStream stream = new MemoryStream(buffer)) { var multipart = new BrowserContextMultipartFormData(x => { x.AddFile("foo", fileName, "text/plain", stream); }); // execute var url = Actions.LogFile.Save(projectId); var response = browser.Post(url, (with) => { with.HttpRequest(); with.FormsAuth(currentUser.Id, new Nancy.Authentication.Forms.FormsAuthenticationConfiguration()); with.MultiPartFormData(multipart); with.FormValue("projectId", projectId.ToString()); }); // assert Assert.AreEqual(HttpStatusCode.OK, response.StatusCode); _dirWrap.Received(1).CreateDirectory(AppContext.BaseDirectory); Assert.IsTrue(File.Exists(filePath)); } }
public void Save_OnExecute_SavesContentsToDisk() { // setup Random r = new Random(); List <UserModel> users = new List <UserModel>(); for (int i = 0; i < r.Next(3, 7); i++) { _userStore.Users.Add(new UserModel() { Id = Guid.NewGuid(), Password = Guid.NewGuid().ToString(), UserName = Guid.NewGuid().ToString(), Role = Guid.NewGuid().ToString(), }); } byte[] key = new byte[20]; List <ConnectionModel> connections = new List <ConnectionModel>(); for (int i = 0; i < r.Next(3, 7); i++) { r.NextBytes(key); _userStore.Connections.Add(new ConnectionModel() { Password = Guid.NewGuid().ToString(), User = Guid.NewGuid().ToString(), Key = Convert.ToBase64String(key), Database = Guid.NewGuid().ToString(), Port = r.Next(1000, 999999), Host = Guid.NewGuid().ToString() }); } _userStore.FilePath = "C:\\Temp\\Test\\users.json"; // execute _userStore.Save(); // assert string contents = JsonConvert.SerializeObject(_userStore, Formatting.Indented); _dirWrap.Received(1).CreateDirectory("C:\\Temp\\Test"); _fileWrap.Received(1).WriteAllText(_userStore.FilePath, contents); }