public void Register_ValidVolunteer() { Person moqPerson = new Person() { Id = 1, UserId = null, FirstName = "Bob", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", ClusterId = 1 }; var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.AddPerson(It.IsAny<Person>())).Returns(moqPerson); VolunteerService service = new VolunteerService(moqDataService.Object); Person actual = service.Register("Bob", "Jones", "*****@*****.**", "555-222-9139", 1, 5); Assert.AreEqual(1, actual.Id); Assert.AreEqual("Bob", actual.FirstName); Assert.AreEqual("Jones", actual.LastName); Assert.AreEqual("*****@*****.**", actual.Email); Assert.AreEqual("555-222-9139", actual.PhoneNumber); Assert.AreEqual(1, actual.ClusterId); }
public void WhenQueryingDisasterAllCommitmentsReturned() { var moqDataService = new Mock <IDataService>(); var disaster = new Disaster { Id = 1, Name = "test", IsActive = false }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List <Disaster> { disaster }.AsQueryable()); moqDataService.Setup(ds => ds.Commitments) .Returns(new List <Commitment> { new Commitment { DisasterId = 1, Id = 1, PersonId = 1, StartDate = new DateTime(2013, 8, 1), EndDate = new DateTime(2013, 9, 1), Disaster = disaster } }.AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); var person = new Person { Id = 1, FirstName = "test", LastName = "tester" }; var results = underTest.RetrieveCommitmentsForDisaster(person, disaster); Assert.IsTrue(results.Count() == 1); Assert.IsTrue(results.Single().Id == 1); }
public void UpdateDetails_PersonEmailAlreadyInUse() { Person moqPersonOne = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139" }; Person moqPersonTwo = new Person() { Id = 2, UserId = null, FirstName = "Stan", LastName = "Smith", Email = "*****@*****.**", PhoneNumber = "111-333-2222" }; List <Person> people = new List <Person>(); people.Add(moqPersonOne); people.Add(moqPersonTwo); var moqDataService = new Mock <IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); // Try to change Cathy Jones' email to Stan Smith's email... service.UpdateDetails(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "Jones" }); }
public ActionResult Create(VolunteerCreate model) { if (!ModelState.IsValid) { return(View(model)); } var service = new VolunteerService(); if (service.CreateVolunteer(model)) { TempData["SaveResult"] = "Volunteer was created."; return(RedirectToAction("Index")); } ; ModelState.AddModelError("", "Volunteer could not be created"); return(View(model)); }
public void UpdateDetails_Valid() { Person moqPerson = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", ClusterId = 1 }; List <Person> people = new List <Person>(); people.Add(moqPerson); var moqDataService = new Mock <IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); moqDataService.Setup(s => s.UpdatePerson(It.IsAny <Person>())).Returns(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "CHANGED", PhoneNumber = "555-222-9139", ClusterId = 1 }); VolunteerService service = new VolunteerService(moqDataService.Object); var actual = service.UpdateDetails(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "CHANGED", PhoneNumber = "555-222-9139", ClusterId = 1 }); // Only Last Name has been updated Assert.AreEqual("CHANGED", actual.LastName); Assert.AreEqual("Cathy", actual.FirstName); }
static void Main(string[] args) { VolunteerRepository volunteerRepository = new VolunteerRepository(); VolunteerService volunteerService = new VolunteerService(volunteerRepository); DonorRepository donorRepository = new DonorRepository(); DonationRepository donationRepository = new DonationRepository(); CaseRepository caseRepository = new CaseRepository(); DonationService donationService = new DonationService(caseRepository, donorRepository, donationRepository); // Volunteer volunteer = volunteerRepository.FindOne("admin", "admin"); // Console.WriteLine("voluntar: "+volunteer.Username); ServerService serverService = new ServerService(volunteerService, donationService); SerialServer server = new SerialServer("127.0.0.1", 55555, serverService); server.Start(); Console.WriteLine("Server started... "); //Console.WriteLine("Press <enter> to exit..."); Console.ReadLine(); }
public void DeleteVolunteerById_Success_Test() { // Arrange int id = 1; // create mock for repository var mock = new Mock <IVolunteerRepository>(); mock.Setup(s => s.DeleteVolunteer(Moq.It.IsAny <int>())).Verifiable(); // service VolunteerService volunteerService = new VolunteerService(); VolunteerService.Repository = mock.Object; // Act volunteerService.DeleteVolunteer(id); // Assert Assert.IsTrue(true); }
public void UpdateVolunteer_Success_Test() { // Arrange VolunteerDTO dto = SampleVolunteerDTO(1); // create mock for repository var mock = new Mock <IVolunteerRepository>(); mock.Setup(s => s.UpdateVolunteer(Moq.It.IsAny <R_Volunteer>())).Verifiable(); // service VolunteerService volunteerService = new VolunteerService(); VolunteerService.Repository = mock.Object; // Act volunteerService.UpdateVolunteer(dto); // Assert Assert.IsNotNull(dto); }
public override void OnActionExecuting(HttpActionContext actionContext) { IEnumerable <string> tokenItem = null; actionContext.Request.Headers.TryGetValues("token", out tokenItem); //没有token if (tokenItem == null) { throwUnanthorizedException(); } string token = tokenItem.ToList()[0]; VolunteerToken volunteerToken = VolunteerService.GetToken(token); //数据库没有此token if (volunteerToken == null) { throwUnanthorizedException(); } //token过期 if (volunteerToken.ExpiredTime < DateTime.UtcNow) { throwUnanthorizedException(); } Volunteer volunteer = VolunteerService.GetVolunteer(volunteerToken.VolunteerId); //volunteer是否存在或有效 if (volunteer == null || volunteer.Status == EnumUserStatus.注销 || volunteer.Status == EnumUserStatus.密码输入错误临时锁定 || volunteer.Status == EnumUserStatus.注册未验证手机) { throwUnanthorizedException(); } SetPrincipal(new VolunteerPrincipal(volunteer)); if (!VolunteerService.UpdateTokenActiveTime(volunteer.Id)) { throwUnanthorizedException(); } base.OnActionExecuting(actionContext); }
public VolunteerService AddVolunteerService(VolunteerService serviceInfo) { using (var db = new SqlConnection(ConnectionString)) { var addServiceInformation = db.QueryFirstOrDefault <VolunteerService>(@" Insert into [volunteerService] (volunteerServiceType) Output inserted.* Values(@volunteerServiceType)", new { volunteerServiceType = serviceInfo.VolunteerServiceType }); if (addServiceInformation != null) { return(addServiceInformation); } } throw new Exception("No user created"); }
public void AddVolunteer_Success_Test() { // Arrange VolunteerDTO dto = SampleVolunteerDTO(1); // create mock for repository var mock = new Mock <IVolunteerRepository>(); mock.Setup(s => s.AddVolunteer(Moq.It.IsAny <R_Volunteer>())).Returns(1); // service VolunteerService volunteerService = new VolunteerService(); VolunteerService.Repository = mock.Object; // Act int id = volunteerService.AddVolunteer(dto); // Assert Assert.AreEqual(1, id); Assert.AreEqual(1, dto.VolunteerId); }
public void GetPersonDetailsForChangeContactInfoReturnsExpectedData() { var personOne = new Person { Id = 1, UserId = 6, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", ClusterId = 1 }; var personTwo = new Person { Id = 2, UserId = 7, FirstName = "Stan", LastName = "Smith", Email = "*****@*****.**", PhoneNumber = "111-333-2222" }; var dataService = new Mock <IDataService>(); var personList = new List <Person> { personOne, personTwo }; dataService.Setup(x => x.Persons).Returns(personList.AsQueryable()); var underTest = new VolunteerService(dataService.Object); var result = underTest.GetPersonDetailsForChangeContactInfo(personTwo.UserId.GetValueOrDefault()); Assert.IsNotNull(result); Assert.AreEqual(personTwo.Email, result.Email); Assert.AreEqual(personTwo.PhoneNumber, result.PhoneNumber); }
public void Register_UsernameAvailable() { User moqUser = new User() { Id = 1, UserName = "******" }; List<User> users = new List<User>(); users.Add(moqUser); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Users).Returns(users.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); //test that the username we created is not available. Assert.IsFalse(service.UsernameAvailable("test123"), "Username created for this test should report as not available."); //test that a different username is available. Assert.IsTrue(service.UsernameAvailable("test456"), "Username that was not added to our data source should report as available."); }
public void GetPersonDetailsForChangeContactInfoReturnsExpectedData() { var personOne = new Person { Id = 1, UserId = 6, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", ClusterId = 1 }; var personTwo = new Person { Id = 2, UserId = 7, FirstName = "Stan", LastName = "Smith", Email = "*****@*****.**", PhoneNumber = "111-333-2222" }; var dataService = new Mock<IDataService>(); var personList = new List<Person> { personOne, personTwo }; dataService.Setup(x => x.Persons).Returns(personList.AsQueryable()); var underTest = new VolunteerService(dataService.Object); var result = underTest.GetPersonDetailsForChangeContactInfo(personTwo.UserId.GetValueOrDefault()); Assert.IsNotNull(result); Assert.AreEqual(personTwo.Email, result.Email); Assert.AreEqual(personTwo.PhoneNumber, result.PhoneNumber); }
public void GetVolunteer_Success_Test() { // Arrange int id = 1; R_Volunteer volunteer = SampleVolunteer(id); // create mock for repository var mock = new Mock <IVolunteerRepository>(); mock.Setup(s => s.GetVolunteer(Moq.It.IsAny <int>())).Returns(volunteer); // service VolunteerService volunteerService = new VolunteerService(); VolunteerService.Repository = mock.Object; // Act VolunteerDTO result = volunteerService.GetVolunteer(id); // Assert Assert.IsNotNull(result); Assert.AreEqual(1, result.VolunteerId); }
public void WhenQueriedAllCommitmentsAreReturned() { var moqDataService = new Mock <IDataService>(); var disaster = new Disaster { Id = 1, Name = "test", IsActive = true }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List <Disaster> { disaster }.AsQueryable()); moqDataService.Setup(ds => ds.Commitments) .Returns(new List <Commitment> { new Commitment { DisasterId = 1, Id = 1, PersonId = 1, StartDate = new DateTime(2013, 8, 1), EndDate = new DateTime(2013, 9, 1), Disaster = disaster } }.AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); const int personId = 1; var results = underTest.RetrieveCommitments(personId, false); Assert.IsTrue(results.Count() == 1); Assert.IsTrue(results.Single().Id == personId); }
public void UpdateDetails_DoesNotClearFirstOrLastNameIfBlank() { var moqPersonOne = new Person { Id = 1, UserId = 6, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139" }; var people = new List <Person> { moqPersonOne }; var moqDataService = new Mock <IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); moqDataService.Setup(s => s.UpdatePerson(It.IsAny <Person>())).Returns((Person p) => p); var service = new VolunteerService(moqDataService.Object); Person actual = service.UpdateDetails(new Person { Id = 1, UserId = 6, FirstName = null, LastName = null, Email = "*****@*****.**", PhoneNumber = "555-222-9139" }); Assert.IsNotNull(actual); Assert.IsFalse(string.IsNullOrEmpty(actual.FirstName), "Expected First Name not to be null or empty."); Assert.IsFalse(string.IsNullOrEmpty(actual.LastName), "Expected Last Name not to be null or empty."); }
public ActionResult Edit(int id) { var service = new VolunteerService(); var detail = service.GetVolunteerById(id); var model = new VolunteerEdit { OrgId = detail.OrgId, FirstName = detail.FirstName, LastName = detail.LastName }; List <OrganizationListItem> Organizations = (new OrganizationService()).GetOrganizations().ToList(); var query = from b in Organizations select new SelectListItem() { Value = b.OrgId.ToString(), Text = b.Name }; ViewBag.OrganizationId = query.ToList(); return(View(model)); }
public ActionResult Edit(int id, VolunteerEdit model) { if (!ModelState.IsValid) { return(View(model)); } if (model.OrgId != id) { ModelState.AddModelError("", "Id Mismatch"); return(View(model)); } var service = new VolunteerService(); if (service.UpdateVolunteer(model)) { TempData["SaveResult"] = "volunteer was updated."; return(RedirectToAction("Index")); } ModelState.AddModelError("", "volunteer could not be updated."); return(View(model)); }
public void WhenQueryingActiveDisastersFilteredCommitmentsReturned() { var moqDataService = new Mock <IDataService>(); var disaster = new Disaster { Id = 1, Name = "test", IsActive = false }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List <Disaster> { disaster }.AsQueryable()); moqDataService.Setup(ds => ds.Commitments) .Returns(new List <Commitment> { new Commitment { DisasterId = 1, Id = 1, PersonId = 1, StartDate = new DateTime(2013, 8, 1), EndDate = new DateTime(2013, 9, 1), Disaster = disaster } }.AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); var personId = 1; var results = underTest.RetrieveCommitments(personId, false); Assert.IsTrue(!results.Any()); }
public void Register_UsernameAvailable() { User moqUser = new User() { Id = 1, UserName = "******" }; List <User> users = new List <User>(); users.Add(moqUser); var moqDataService = new Mock <IDataService>(); moqDataService.Setup(s => s.Users).Returns(users.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); //test that the username we created is not available. Assert.IsFalse(service.UsernameAvailable("test123"), "Username created for this test should report as not available."); //test that a different username is available. Assert.IsTrue(service.UsernameAvailable("test456"), "Username that was not added to our data source should report as available."); }
public void Register_NullLastName() { VolunteerService service = new VolunteerService(new Mock<IDataService>().Object); service.Register("first", "", "email", "555-333-1111", 1, 2); }
public void UpdateDetails_DoesNotClearFirstOrLastNameIfBlank() { var moqPersonOne = new Person { Id = 1, UserId = 6, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139" }; var people = new List<Person> {moqPersonOne}; var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); moqDataService.Setup(s => s.UpdatePerson(It.IsAny<Person>())).Returns((Person p) => p); var service = new VolunteerService(moqDataService.Object); Person actual = service.UpdateDetails(new Person { Id = 1, UserId = 6, FirstName = null, LastName = null, Email = "*****@*****.**", PhoneNumber = "555-222-9139" }); Assert.IsNotNull(actual); Assert.IsFalse(string.IsNullOrEmpty(actual.FirstName), "Expected First Name not to be null or empty."); Assert.IsFalse(string.IsNullOrEmpty(actual.LastName), "Expected Last Name not to be null or empty."); }
public void AddVolunteerTest() { var serv = new VolunteerService(new MVCHContext()); serv.AddVolunteer(new Volunteer { FirstName = "Cedrick", LastName = "Sederiosa", MiddleInitial = "S.", Address = "3212 Kahel St., Sto. Domingo II, Pampanga", City = "Davao City", State = "Philippines", BirthDate = DateTime.ParseExact("09/02/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+639495940509", Zip = "8000", StartDate = DateTime.Now, EndDate = DateTime.Now, HoursWorked = 0, SupervisorId = "PER-000011", WorkUnitId = "WKS-000001" }); serv.AddVolunteer(new Volunteer { FirstName = "John", LastName = "Branzuela", MiddleInitial = "C.", Address = "Sasa Dose", City = "Davao City", State = "Philippines", BirthDate = DateTime.ParseExact("10/04/1999", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000012", WorkUnitId = "WKS-000002" }); serv.AddVolunteer(new Volunteer { FirstName = "Nathan", LastName = "Naanep", MiddleInitial = "G.", Address = "Ilonggo", City = "Koronadal City", State = "Philippines", BirthDate = DateTime.ParseExact("03/25/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000013", WorkUnitId = "WKS-000003" }); serv.AddVolunteer(new Volunteer { FirstName = "Mecca", LastName = "Umapas", MiddleInitial = "S.", Address = "Surigao", City = "Surigao", State = "Philippines", BirthDate = DateTime.ParseExact("10/30/1999", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000014", WorkUnitId = "WKS-000004" }); serv.AddVolunteer(new Volunteer { FirstName = "Yoshi", LastName = "Aizawa", MiddleInitial = "M.", Address = "Mintal", City = "Davao City", State = "Japan", BirthDate = DateTime.ParseExact("12/13/1999", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000015", WorkUnitId = "WKS-000005" }); serv.AddVolunteer(new Volunteer { FirstName = "Adrian", LastName = "De Guzman", MiddleInitial = "A.", Address = "Ilonggo", City = "Tacurong City", State = "Philippines", BirthDate = DateTime.ParseExact("06/08/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000016", WorkUnitId = "WKS-000006" }); serv.AddVolunteer(new Volunteer { FirstName = "Abigail", LastName = "M.", MiddleInitial = "M.", Address = "3212 Kahel St., Sto. Domingo II, Pampanga", City = "Davao City", State = "Philippines", BirthDate = DateTime.ParseExact("09/02/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000017", WorkUnitId = "WKS-000007" }); serv.AddVolunteer(new Volunteer { FirstName = "Anna", LastName = "O.", MiddleInitial = "O.", Address = "3212 Kahel St., Sto. Domingo II, Pampanga", City = "Davao City", State = "Philippines", BirthDate = DateTime.ParseExact("09/02/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000018", WorkUnitId = "WKS-000008" }); serv.AddVolunteer(new Volunteer { FirstName = "Katriana", LastName = "B.", MiddleInitial = "B.", Address = "3212 Kahel St., Sto. Domingo II, Pampanga", City = "Davao City", State = "Philippines", BirthDate = DateTime.ParseExact("09/02/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000019", WorkUnitId = "WKS-000009" }); serv.AddVolunteer(new Volunteer { FirstName = "Dexie", LastName = "Badilles", MiddleInitial = ".", Address = "Sasa", City = "Davao City", State = "Philippines", BirthDate = DateTime.ParseExact("09/02/2000", "mm/dd/yyyy", new CultureInfo("en-PH")), Email = "*****@*****.**", PhoneNumber = "+63969696969", Zip = "8000", StartDate = DateTime.MinValue, HoursWorked = DateTime.Now.Hour - DateTime.MinValue.Hour, SupervisorId = "PER-000020", WorkUnitId = "WKS-000010" }); }
public void Register_NullLastName() { VolunteerService service = new VolunteerService(new Mock <IDataService>().Object); service.Register("first", "", "email", "555-333-1111", 2); }
public IHttpActionResult PostLogin(Login model) { //验证Android手机终端 IEnumerable <string> headerUuid = null; ActionContext.Request.Headers.TryGetValues("uuid", out headerUuid); if (headerUuid == null) { return(BadRequest("请从Android手机操作")); } string uuid = null; if (headerUuid != null) { uuid = headerUuid.ToList()[0]; } //验证数据 if (model == null) { return(BadRequest("请填写登录信息")); } if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.First(s => s.Errors.Count > 0).Errors[0].ErrorMessage)); } Volunteer volunteer = db.Volunteers.FirstOrDefault(s => s.MobileNumber == model.MobileNumber && s.Status != EnumUserStatus.注销); //应为0个或1个 //验证是否已注册 if (volunteer == null) { return(BadRequest("该手机号码未注册")); } //验证账户是否锁定 if (volunteer.Status == EnumUserStatus.密码输入错误临时锁定) { if (volunteer.LockoutEndTime < DateTime.Now) { volunteer.Status = EnumUserStatus.正常; volunteer.LockoutStartTime = null; volunteer.LockoutDuration = 0; db.SaveChanges(); } else { return(BadRequest("账户被锁定")); } } //验证密码 if (volunteer.Password != SecurityHelper.MD5Hash(model.PassWord)) { return(BadRequest("密码不正确")); } //更新uuid if (uuid != null) { volunteer.Uuid = uuid; } VolunteerToken volunteerToken = db.VolunteerTokens.FirstOrDefault(s => s.VolunteerId == volunteer.Id); DateTime now = DateTime.UtcNow; string token = SecurityHelper.MD5Hash(volunteer.Id.ToString() + now.ToString() + Guid.NewGuid().ToString()); if (volunteerToken == null) { volunteerToken = new VolunteerToken { VolunteerId = volunteer.Id, CreateTime = now, ActiveTime = now, Token = token, Validity = 30 }; try { db.VolunteerTokens.Add(volunteerToken); db.Entry(volunteerToken).State = EntityState.Added; db.SaveChanges(); } catch { db.Entry(volunteerToken).State = EntityState.Unchanged; return(BadRequest("操作发生错误")); } } else { volunteerToken.ActiveTime = now; volunteerToken.Token = token; volunteerToken.Validity = 30; db.Entry(volunteerToken).State = EntityState.Modified; try { db.SaveChanges(); } catch { db.Entry(volunteerToken).State = EntityState.Unchanged; return(BadRequest("操作发生错误")); } } return(Ok(new { Token = token, Volunteer = VolunteerService.FormatUser(volunteer) })); }
public void Register_VolunteerAlreadyExists() { Person moqPerson = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", ClusterId = 1 }; List<Person> people = new List<Person>(); people.Add(moqPerson); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); Person actual = service.Register("Cathy", "Jones", "*****@*****.**", "555-222-9139 ext 33", 1, 6); Assert.AreEqual("Cathy", actual.FirstName); Assert.AreEqual("Jones", actual.LastName); Assert.AreEqual("*****@*****.**", actual.Email); Assert.AreEqual("555-222-9139 ext 33", actual.PhoneNumber); Assert.AreEqual(1, actual.ClusterId); }
public void WhenQueryingReturnCommitmentsOnlyForThisUser() { var moqDataService = new Mock<IDataService>(); var disaster = new Disaster { Id = 1, Name = "test", IsActive = true }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List<Disaster> { disaster }.AsQueryable()); moqDataService.Setup(ds => ds.Commitments) .Returns(new List<Commitment> { new Commitment { DisasterId=1, Id = 1, PersonId=1, StartDate=new DateTime(2013, 8, 1), EndDate = new DateTime(2013, 9, 1), Disaster=disaster }, new Commitment { DisasterId=1, Id = 2, PersonId=2, StartDate=new DateTime(2013, 8, 1), EndDate = new DateTime(2013, 9, 1) } }.AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); var person = new Person { Id = 1, FirstName = "test", LastName = "tester" }; var results = underTest.RetrieveCommitmentsForDisaster(person, disaster); Assert.IsTrue(results.Count() == 1); }
public void UpdateDetails_PersonEmailAlreadyInUse() { Person moqPersonOne = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139" }; Person moqPersonTwo = new Person() { Id = 2, UserId = null, FirstName = "Stan", LastName = "Smith", Email = "*****@*****.**", PhoneNumber = "111-333-2222" }; List<Person> people = new List<Person>(); people.Add(moqPersonOne); people.Add(moqPersonTwo); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); // Try to change Cathy Jones' email to Stan Smith's email... service.UpdateDetails(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "Jones" }); }
public void UpdateDetails_NullPerson() { var moqDataService = new Mock<IDataService>(); VolunteerService service = new VolunteerService(moqDataService.Object); service.UpdateDetails(null); }
public ServerService(VolunteerService volunteerService, DonationService donationService) { this.volunteerService = volunteerService; this.donationService = donationService; observers = new List <ITeledonObserver>(); }
public void Register_EmailAlreadyInUse_ReturnTrue() { Person moqPerson = new Person() { Id = 1, Email = "*****@*****.**" }; List<Person> people = new List<Person>(); people.Add(moqPerson); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); Assert.IsTrue(service.EmailAlreadyInUse("*****@*****.**")); }
public void Register_NullPhoneNumber() { VolunteerService service = new VolunteerService(new Mock <IDataService>().Object); service.Register("first", "last", "email", "", 3); }
public void WhenNoCommitmentsExistAnywhereEmptyCollectionIsReturned() { var moqDataService = new Mock<IDataService>(); moqDataService.Setup(ds => ds.Commitments) .Returns(new List<Commitment>().AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); var disaster = new Disaster { Id = 1, Name = "test", IsActive = true }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List<Disaster> { disaster }.AsQueryable()); const int personId = 1; var results = underTest.RetrieveCommitments(personId, false); Assert.IsTrue(!results.Any()); }
public void UpdateDetails_ChangeEmailToUnusuedEmail_DoesNotThrowPersonEmailAlreadyInUseException() { Person moqPersonOne = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139" }; List<Person> people = new List<Person>(); people.Add(moqPersonOne); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); // Try to change Cathy Jones' email to something not in use... service.UpdateDetails(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "Jones" }); }
public void Register_NullPhoneNumber() { VolunteerService service = new VolunteerService(new Mock<IDataService>().Object); service.Register("first", "last", "email", "", 1, 3); }
public Person Create_a_volunteer() { var volunteerService = new VolunteerService(_dataService); return(volunteerService.Register("Sally", "Struthers", "*****@*****.**", "890-1230-4567", 100)); }
public void UpdateDetails_PersonNotFound() { Person moqPerson = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139" }; List<Person> people = new List<Person>(); people.Add(moqPerson); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); VolunteerService service = new VolunteerService(moqDataService.Object); service.UpdateDetails(new Person() { Id = 25, Email = "*****@*****.**" }); }
public void WhenUserIsNullGetAllCommitmentsThrowsNullArgumentException() { var moqDataService = new Mock<IDataService>(); var underTest = new VolunteerService(moqDataService.Object); var results = underTest.RetrieveCommitments(default(Person), false); }
public void UpdateDetails_Valid() { Person moqPerson = new Person() { Id = 1, UserId = null, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", ClusterId = 1 }; List<Person> people = new List<Person>(); people.Add(moqPerson); var moqDataService = new Mock<IDataService>(); moqDataService.Setup(s => s.Persons).Returns(people.AsQueryable()); moqDataService.Setup(s => s.UpdatePerson(It.IsAny<Person>())).Returns(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "CHANGED", PhoneNumber = "555-222-9139", ClusterId = 1 }); VolunteerService service = new VolunteerService(moqDataService.Object); var actual = service.UpdateDetails(new Person() { Id = 1, Email = "*****@*****.**", FirstName = "Cathy", LastName = "CHANGED", PhoneNumber = "555-222-9139", ClusterId = 1 }); // Only Last Name has been updated Assert.AreEqual("CHANGED", actual.LastName); Assert.AreEqual("Cathy", actual.FirstName); }
public IHttpActionResult PostRegister(Register model) { DateTime now = DateTime.Now; //验证Andriod手机端 IEnumerable <string> headUuid = null; ActionContext.Request.Headers.TryGetValues("uuid", out headUuid); string uuid = null; if (headUuid != null) { uuid = headUuid.ToList()[0]; } //验证数据 if (model == null) { return(BadRequest("请填写注册信息")); } if (!ModelState.IsValid) { return(BadRequest(ModelState.Values.First(s => s.Errors.Count > 0).Errors[0].ErrorMessage)); } Volunteer volunteer = db.Volunteers.FirstOrDefault(s => s.MobileNumber == model.MobileNumber && s.Status != EnumUserStatus.注销); //未点击获取验证码 if (volunteer == null) { return(BadRequest("请获取验证码")); } //手机号码已注册 if (volunteer.Status == EnumUserStatus.注册未验证手机) { return(BadRequest("该手机号码已注册")); } //数据库中不存在验证码 if (string.IsNullOrEmpty(volunteer.SmsCaptcha)) { return(BadRequest("请获取验证码")); } //验证码用途不正确 if (!volunteer.SmsCaptchaUsedFor.HasValue || volunteer.SmsCaptchaUsedFor != EnumCaptchaUsedFor.志愿者注册) { return(BadRequest("验证码无效,请重新获取验证码")); } //验证码失效 if (volunteer.SmsCaptchaExpiredTime > now) { return(BadRequest("验证码失效,请重新获取")); } //验证码错误 if (volunteer.SmsCaptcha != model.Captcha) { return(BadRequest("验证码错误")); } //注册,生成token volunteer.Uuid = uuid; volunteer.Password = SecurityHelper.MD5Hash(model.PassWord); volunteer.SmsCaptcha = null; volunteer.SmsCaptchaExpiredTime = null; volunteer.SmsCaptchaUsedFor = null; volunteer.Status = EnumUserStatus.注册中; volunteer.RegisterTime = now; db.Entry(volunteer).State = EntityState.Modified; try { db.SaveChanges(); VolunteerToken volunteerToken = db.VolunteerTokens.FirstOrDefault(s => s.VolunteerId == volunteer.Id); string token = SecurityHelper.MD5Hash(volunteer.Id.ToString() + now.ToString() + Guid.NewGuid().ToString()); if (volunteerToken == null) { volunteerToken = new VolunteerToken { VolunteerId = volunteer.Id, Token = token, CreateTime = now, ActiveTime = now, Validity = 30 }; db.VolunteerTokens.Add(volunteerToken); db.SaveChanges(); } else { volunteerToken.ActiveTime = now; volunteerToken.Token = token; volunteerToken.Validity = 30; db.Entry(volunteerToken).State = EntityState.Modified; db.SaveChanges(); } return(Ok( new { Token = token, Volunteer = VolunteerService.FormatUser(volunteer) } )); } catch (Exception ex) { db.Entry(volunteer).State = EntityState.Unchanged; return(BadRequest("操作发生错误")); } }
public void Register_NullCluster() { VolunteerService service = new VolunteerService(new Mock<IDataService>().Object); service.Register("first", "last", "email", "555-333-1111", 0, 4); }
public void WhenDisasterIsNullGetCommitmentsThrowsNullArgumentException() { var moqDataService = new Mock<IDataService>(); var underTest = new VolunteerService(moqDataService.Object); var person = new Person { Id = 1, FirstName = "test", LastName = "tester" }; var results = underTest.RetrieveCommitmentsForDisaster(person, default(Disaster)); }
public void WhenNoCommitmentsExistEmptyCollectionIsReturned() { var moqDataService = new Mock<IDataService>(); moqDataService.Setup(ds => ds.Commitments) .Returns(new List<Commitment>().AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); var person = new Person { Id = 1, FirstName = "test", LastName = "tester" }; var disaster = new Disaster { Id = 1, Name = "test", IsActive = true }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List<Disaster> { disaster }.AsQueryable()); var results = underTest.RetrieveCommitmentsForDisaster(person, disaster); Assert.IsTrue(results.Count() == 0); }
public void Register_NullEmail() { VolunteerService service = new VolunteerService(new Mock <IDataService>().Object); service.Register("first", "last", "", "555-333-1111", 3); }
public void Register_NullEmail() { VolunteerService service = new VolunteerService(new Mock<IDataService>().Object); service.Register("first", "last", "", "555-333-1111", 1, 3); }
public void WhenPersonIdDoesNotExistReturnsEmptyList() { var moqDataService = new Mock<IDataService>(); moqDataService.Setup(service => service.Commitments).Returns(new List<Commitment>().AsQueryable); var underTest = new VolunteerService(moqDataService.Object); var results = underTest.RetrieveCommitments(0, false); }
public void GetPersonDetailsForChangeContactInfoThrowsExpectedPersonNotFoundException() { var personOne = new Person { Id = 1, UserId = 6, FirstName = "Cathy", LastName = "Jones", Email = "*****@*****.**", PhoneNumber = "555-222-9139", }; var personTwo = new Person { Id = 2, UserId = 7, FirstName = "Stan", LastName = "Smith", Email = "*****@*****.**", PhoneNumber = "111-333-2222" }; var dataService = new Mock<IDataService>(); var personList = new List<Person> { personOne, personTwo }; dataService.Setup(x => x.Persons).Returns(personList.AsQueryable()); var underTest = new VolunteerService(dataService.Object); var result = underTest.GetPersonDetailsForChangeContactInfo(8); }
public void WhenQueryingInactiveDisastersAllCommitmentsReturned() { var moqDataService = new Mock<IDataService>(); var disaster = new Disaster { Id = 1, Name = "test", IsActive = false }; moqDataService.Setup(ds => ds.Disasters) .Returns(new List<Disaster> { disaster }.AsQueryable()); moqDataService.Setup(ds => ds.Commitments) .Returns(new List<Commitment> { new Commitment { DisasterId=1, Id = 1, PersonId=1, StartDate=new DateTime(2013, 8, 1), EndDate = new DateTime(2013, 9, 1), Disaster=disaster } }.AsQueryable()); var underTest = new VolunteerService(moqDataService.Object); const int personId = 1; var results = underTest.RetrieveCommitments(personId, true); Assert.IsTrue(results.Count() == 1); Assert.IsTrue(results.Single().Id == 1); }
public void GetVolunteerListAdvancedSearch_Success_Test() { // Arrange string name = null; int? gender = null; System.DateTime?birthDateFrom = null; System.DateTime?birthDateTo = null; string occupation = null; string employer = null; string phone = null; string email = null; string identityCardNumber = null; int? countryId = null; string friendOrFamilyContact = null; int? photo = null; int? addressId = null; bool? hasCar = null; bool? hasDriverLicense = null; bool? hasBike = null; string vehicleMake = null; string vehicleModel = null; bool? active = null; //int pageIndex = 0; int pageSize = 10; // list IList <R_Volunteer> list = new List <R_Volunteer>(); for (int i = 1; i <= pageSize; i++) { list.Add(SampleVolunteer(i)); } // create mock for repository var mock = new Mock <IVolunteerRepository>(); mock.Setup(s => s.GetVolunteerListAdvancedSearch( Moq.It.IsAny <string>() // name , Moq.It.IsAny <int?>() // gender , Moq.It.IsAny <System.DateTime?>() // birthDateFrom , Moq.It.IsAny <System.DateTime?>() // birthDateTo , Moq.It.IsAny <string>() // occupation , Moq.It.IsAny <string>() // employer , Moq.It.IsAny <string>() // phone , Moq.It.IsAny <string>() // email , Moq.It.IsAny <string>() // identityCardNumber , Moq.It.IsAny <int?>() // countryId , Moq.It.IsAny <string>() // friendOrFamilyContact , Moq.It.IsAny <int?>() // photo , Moq.It.IsAny <int?>() // addressId , Moq.It.IsAny <bool?>() // hasCar , Moq.It.IsAny <bool?>() // hasDriverLicense , Moq.It.IsAny <bool?>() // hasBike , Moq.It.IsAny <string>() // vehicleMake , Moq.It.IsAny <string>() // vehicleModel , Moq.It.IsAny <bool?>() // active )).Returns(list); // service VolunteerService volunteerService = new VolunteerService(); VolunteerService.Repository = mock.Object; // Act var resultList = volunteerService.GetVolunteerListAdvancedSearch( name , gender , birthDateFrom , birthDateTo , occupation , employer , phone , email , identityCardNumber , countryId , friendOrFamilyContact , photo , addressId , hasCar , hasDriverLicense , hasBike , vehicleMake , vehicleModel , active ); VolunteerDTO result = resultList.FirstOrDefault(); // Assert Assert.IsNotNull(result); Assert.AreEqual(1, result.VolunteerId); }