public void should_add_reply() { // Arrange _app.MockUser(); var(topic, userId) = CreateTopic(_app); // 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(userId); replies[0].Content.ShouldEqual("my reply"); var dbContext = _app.GetService <ApplicationDbContext>(); dbContext.Entry(topic).Reload(); topic.ReplyCount.ShouldEqual(1); topic.LastRepliedAt.ShouldNotBeNull(); var span = DateTime.UtcNow - topic.LastRepliedAt.Value; Assert.True(span.TotalSeconds > 0); Assert.True(span.TotalSeconds < 10); }
public static async Task ClassInitAsync(TestContext context) { app = new(); await app.InitializeAsync(context); await app.OpenAsync(); userCategoryCollection = app.GetService(typeof(IUserCategoryCollection)) as IUserCategoryCollection; userCollection = app.GetService(typeof(IUserCollection)) as IUserCollection; userContext = app.GetService(typeof(IUserContext)) as IUserContext; expiredAuthentication = app.ExpiredAuthentication; }
public static async Task InitializeAsync(this TestContext context, TestApplication app) { context.Properties.Add(appKey, app); context.Properties.Add(cremaHostKey, app.GetService(typeof(ICremaHost))); context.Properties.Add(authenticationKey, new HashSet <Authentication>()); await Task.Delay(1); }
public async Task should_signin_user_and_redirect_when_signin_with_valid_user() { // Arrange ClaimsPrincipal signedInClaimsPrincipal = null; var authService = new Mock <IAuthenticationService>(); authService.Setup(auth => auth.SignInAsync(It.IsAny <HttpContext>(), It.IsAny <string>(), It.IsAny <ClaimsPrincipal>(), It.IsAny <AuthenticationProperties>())) .Returns(Task.CompletedTask) .Callback((HttpContext ctx, string scheme, ClaimsPrincipal claimsPrincipal, AuthenticationProperties props) => { signedInClaimsPrincipal = claimsPrincipal; }) .Verifiable(); ReplacableServiceProvider.Replace(services => { services.AddSingleton(authService.Object); }); var accountCtrl = _myApp.CreateController <AccountController>(); var userManager = _myApp.GetService <UserManager <User> >(); var userRepo = _myApp.GetService <IRepository <User> >(); const string password = "******"; await userManager.CreateAsync(new User { UserName = "******", DisplayName = "Jim Green", CreatedAtUtc = DateTime.UtcNow }, password); // Act var userModel = new SigninUserViewModel { UserName = "******", Password = password }; var sigininResult = await accountCtrl.DoSignin(userModel, null); // Assert Assert.True(accountCtrl.ModelState.IsValid); sigininResult.IsType <RedirectResult>(); authService.Verify(); Assert.Equal("jim", signedInClaimsPrincipal.ToDiscussionUser(userRepo).UserName); }
public void should_serve_topic_list_on_page() { var topicItems = new[] { new Topic { Title = "dummy topic 1", Type = TopicType.Discussion }, new Topic { Title = "dummy topic 2", Type = TopicType.Discussion }, new Topic { Title = "dummy topic 3", Type = TopicType.Discussion }, }; var repo = _myApp.GetService <IRepository <Topic> >(); foreach (var item in topicItems) { repo.Save(item); } var topicController = _myApp.CreateController <TopicController>(); var topicListResult = topicController.List() as ViewResult; var listViewModel = topicListResult.ViewData.Model as TopicListViewModel; listViewModel.ShouldNotBeNull(); var topicList = listViewModel.Topics; topicList.ShouldContain(t => t.Title == "dummy topic 1"); topicList.ShouldContain(t => t.Title == "dummy topic 2"); topicList.ShouldContain(t => t.Title == "dummy topic 3"); }
public static async Task ClassInitAsync(TestContext context) { app = new(); await app.InitializeAsync(context); await app.OpenAsync(); dataBaseContext = app.GetService(typeof(IDataBaseContext)) as IDataBaseContext; }
public async Task UsersKicked_TestAsync() { var cremaHost = app.GetService(typeof(ICremaHost)) as ICremaHost; var authentication = await this.TestContext.LoginRandomAsync(Authority.Admin); var authentication1 = await this.TestContext.LoginRandomAsync(); var authentication2 = await this.TestContext.LoginRandomAsync(); var user1 = await userCollection.GetUserAsync(authentication1.ID); var user2 = await userCollection.GetUserAsync(authentication2.ID); var actualUserID = string.Empty; var actualMessage = string.Empty; var expectedMessage = RandomUtility.NextString(); await userCollection.AddUsersKickedEventHandlerAsync(UserCollection_UsersKicked); await user1.KickAsync(authentication, expectedMessage); Assert.AreEqual(user1.ID, actualUserID); Assert.AreEqual(expectedMessage, actualMessage); await userCollection.RemoveUsersKickedEventHandlerAsync(UserCollection_UsersKicked); await user2.KickAsync(authentication, RandomUtility.NextString()); Assert.AreEqual(user1.ID, actualUserID); Assert.AreEqual(expectedMessage, actualMessage); Assert.AreNotEqual(user2.ID, actualUserID); void UserCollection_UsersKicked(object sender, ItemsEventArgs <IUser> e) { var user = e.Items.Single(); actualUserID = user.ID; actualMessage = (e.MetaData as string[]).Single(); } }
internal static (Topic, int) CreateTopic(TestApplication testApplication) { var userId = testApplication.User.ExtractUserId().Value; var topic = new Topic { Title = "test topic", Content = "topic content", CreatedBy = userId, Type = TopicType.Discussion }; testApplication.GetService <IRepository <Topic> >().Save(topic); return(topic, userId); }
public async Task should_reject_post_request_without_valid_anti_forgery_token() { // arrange var username = Guid.NewGuid().ToString("N").Substring(4, 8); var password = "******"; var tokens = _theApp.GetAntiForgeryTokens(); // Act var request = _theApp.Server.CreateRequest("/register") .WithFormContent(new Dictionary <string, string>() { { "UserName", username }, { "Password", password }, { "__RequestVerificationToken", "some invalid token" } }) .WithCookie(tokens.Cookie); var response = await request.PostAsync(); // assert response.StatusCode.ShouldEqual(HttpStatusCode.BadRequest); var isRegistered = _theApp.GetService <IRepository <User> >().All().Any(u => u.UserName == username); isRegistered.ShouldEqual(false); }
public async Task should_be_able_to_register_new_user() { // arrange var username = Guid.NewGuid().ToString("N").Substring(4, 8); var password = "******"; // Act var request = _theApp.Server.CreateRequest("/register") .WithFormContent(new Dictionary <string, string>() { { "UserName", username }, { "Password", password }, { "__RequestVerificationToken", _antiForgeryTokens.VerificationToken } }) .WithCookie(_antiForgeryTokens.Cookie); var response = await request.PostAsync(); // assert response.StatusCode.ShouldEqual(HttpStatusCode.Redirect); var isRegistered = _theApp.GetService <IRepository <User> >().All().Any(u => u.UserName == username); isRegistered.ShouldEqual(true); }
public AccountControllerSpecs(TestApplication app) { _myApp = app.Reset(); _userRepo = _myApp.GetService <IRepository <User> >(); }