public void should_create_topic() { _app.MockUser(); var topicController = _app.CreateController <TopicController>(); var model = new TopicCreationModel() { Title = "first topic you created", Content = "**This is the content of this markdown**\r\n* markdown content is greate*", Type = TopicType.Job }; topicController.CreateTopic(model); var repo = _app.GetService <IRepository <Topic> >(); var allTopics = repo.All().ToList(); var createdTopic = allTopics.Find(topic => topic.Title == model.Title); createdTopic.ShouldNotBeNull(); createdTopic.Title.ShouldEqual(model.Title); createdTopic.Content.ShouldEqual(model.Content); createdTopic.Type.ShouldEqual(TopicType.Job); createdTopic.CreatedBy.ShouldEqual(_app.GetDiscussionUser().Id); var createdAt = DateTime.UtcNow - createdTopic.CreatedAtUtc; Assert.True(createdAt.TotalMilliseconds >= 0); Assert.True(createdAt.TotalMinutes < 2); createdTopic.LastRepliedAt.ShouldBeNull(); createdTopic.ReplyCount.ShouldEqual(0); createdTopic.ViewCount.ShouldEqual(0); }
public void should_create_topic() { var user = _app.MockUser(); var topicController = _app.CreateController <TopicController>(); var model = new TopicCreationModel() { Title = "first topic you created", Content = "**This is the content of this markdown**\r\n* markdown content is greate*", Type = TopicType.Job }; var actionResult = topicController.CreateTopic(model); var topicCreated = VerifyTopicCreated(actionResult, model); topicCreated.LastRepliedAt.ShouldBeNull(); topicCreated.ReplyCount.ShouldEqual(0); topicCreated.ViewCount.ShouldEqual(0); var topicCreatedLog = _app.GetLogs().FirstOrDefault(log => log.Message.Contains("创建话题成功")); Assert.NotNull(topicCreatedLog); Assert.Contains($"UserId = {user.Id}", topicCreatedLog.Message); Assert.Contains($"Id = {topicCreated.Id}", topicCreatedLog.Message); Assert.Contains(topicCreated.Title, topicCreatedLog.Message); }
public void should_not_accept_create_topic_request_with_invalid_post_data() { _app.Path("/topics") .Post() .ShouldFail(_app.MockUser()) .WithResponse(res => { res.StatusCode.ShouldEqual(HttpStatusCode.BadRequest); }); }
public void should_serve_convert_md2html_api_correctly() { _app.Path("/api/common/md2html") .Post() .WithForm(new { markdown = "# 中文的 title" }) .ShouldSuccess(_app.MockUser()) .WithApiResult((api, result) => result["html"].ToString() .ShouldContain("<h2>中文的 title</h2>")) .And .ShouldFail(_app.NoUser()) .WithSigninRedirect(); }
public void should_add_reply() { // Arrange var user = _app.MockUser(); var topic = _app.NewTopic().WithAuthor(user).Create(); // Act var replyController = _app.CreateController <ReplyController>(); replyController.Reply(topic.Id, new ReplyCreationModel { Content = "my reply" }); // Assert var replies = _app.GetService <IRepository <Reply> >() .All() .Where(c => c.TopicId == topic.Id) .ToList(); replies.Count.ShouldEqual(1); replies[0].TopicId.ShouldEqual(topic.Id); replies[0].CreatedBy.ShouldEqual(user.Id); replies[0].Content.ShouldEqual("my reply"); var dbContext = _app.GetService <ApplicationDbContext>(); dbContext.Entry(topic).Reload(); topic.ReplyCount.ShouldEqual(1); topic.LastRepliedByUser.ShouldNotBeNull(); topic.LastRepliedAuthor.ShouldNotBeNull(); topic.LastRepliedAt.ShouldNotBeNull(); var span = DateTime.UtcNow - topic.LastRepliedAt.Value; Assert.True(span.TotalSeconds > 0); Assert.True(span.TotalSeconds < 10); var replyCreatedLog = _app.GetLogs().FirstOrDefault(log => log.Message.Contains("添加回复成功")); Assert.NotNull(replyCreatedLog); Assert.Contains($"UserId = {user.Id}", replyCreatedLog.Message); Assert.Contains($"TopicId = {topic.Id}", replyCreatedLog.Message); Assert.Contains($"ReplyId = {replies[0].Id}", replyCreatedLog.Message); }
public async Task should_upload_file() { _app.MockUser(); var file = MockFile("test-file.txt"); var commonController = _app.CreateController <CommonController>(); commonController.Url = CreateMockUrlHelper("file-link"); var requestResult = await commonController.UploadFile(file, "avatar"); Assert.NotNull(requestResult); Assert.True(requestResult.HasSucceeded); dynamic uploadResult = requestResult.Result; int fileId = uploadResult.FileId; var fileRecord = _fileRepo.Get(fileId); Assert.Equal("avatar", fileRecord.Category); Assert.Equal("test-file.txt", fileRecord.OriginalName); string publicUrl = uploadResult.PublicUrl; Assert.Equal("file-link", publicUrl); }
public async Task should_sign_out() { var authService = new Mock <IAuthenticationService>(); authService.Setup(auth => auth.SignOutAsync(It.IsAny <HttpContext>(), It.IsAny <string>(), It.IsAny <AuthenticationProperties>())).Returns(Task.CompletedTask).Verifiable(); _app.OverrideServices(services => services.AddSingleton(authService.Object)); _app.MockUser(); var accountCtrl = _app.CreateController <AccountController>(); var signOutResult = await accountCtrl.DoSignOut(); Assert.NotNull(signOutResult); signOutResult.IsType <RedirectResult>(); authService.Verify(); }
public async Task should_update_display_name() { var user = _theApp.MockUser(); var settingViewModel = new UserSettingsViewModel { DisplayName = StringUtility.Random() }; var userCtrl = _theApp.CreateController <UserController>(); var result = await userCtrl.DoSettings(settingViewModel); ShouldBeRedirectResult(result); _theApp.ReloadEntity(user); Assert.Equal(settingViewModel.DisplayName, user.DisplayName); Assert.Null(user.EmailAddress); Assert.False(user.EmailAddressConfirmed); }