public async void DoesGetResetDBInDev_GetCallSentInDevMode_DBReset() { _webApp.Load(_webApp.DBContext); AdminFixture.Load(_webApp.DBContext); //setting up login token var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PostAsync("api/Admins", StringContent); string adminToken = await response.Content.ReadAsStringAsync(); _client.SetBearerToken(adminToken); //New Region object to add to DB Region newRegion = new Region { regionName = "DB Reset Region", frequency = 10, firstDate = DateTime.Now.AddDays(1.0), inactive = false }; //convert the Region to a string for posting to the database StringContent = new StringContent(JsonConvert.SerializeObject(newRegion), Encoding.UTF8, "application/json"); //send a post request to the api with the Subscriber object in JSON form response = await _client.PostAsync("api/Regions", StringContent); //get the response from the api and read it into a string string responseString = await response.Content.ReadAsStringAsync(); //deserialize the response into a Region object Region postRegion = JsonConvert.DeserializeObject <Region>(responseString); //created status code is 201 Assert.Equal(HttpStatusCode.Created, response.StatusCode); int regionID = postRegion.regionID; //Get returned region's ID //Attempts to find a single region with the appropriate name and date Region dbRegion = _webApp.DBContext.Region.AsNoTracking().Where(reg => reg.regionID == regionID).Single(); //Confirms only one region is found Assert.Equal(newRegion.regionName, dbRegion.regionName); Assert.Equal(newRegion.frequency, dbRegion.frequency); Assert.Equal(newRegion.firstDate, dbRegion.firstDate); Assert.Equal(newRegion.inactive, dbRegion.inactive); //Send a PUT request to API to update the Route to completed response = await _client.GetAsync("api/WebApp/reloadDB"); responseString = await response.Content.ReadAsStringAsync(); Assert.Equal(HttpStatusCode.OK, response.StatusCode); Assert.Equal("Database reset", responseString); List <Region> regions = _webApp.DBContext.Region.AsNoTracking().Where(reg => reg.regionID == regionID).ToList(); Assert.Empty(regions); }
public async void AddSubscriber_InvalidSubscriber_SubscriberNotAddedToDB() { AdminFixture.Load(_webApp.DBContext); //setting up login token var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PostAsync("api/Admins", StringContent); string adminToken = await response.Content.ReadAsStringAsync(); _client.SetBearerToken(adminToken); //Reload the database. //_webApp.ReloadDBFixtures(); //SubscriberFixture.Reload(_webApp.DBContext); _webApp.Load(_webApp.DBContext); //create a new subscriber to add to the database with valid fields Subscriber NewSubscriber = _webApp.DBContext.Subscriber.ToList()[0]; NewSubscriber.phoneNumber = "l"; //convert the subsciber to a string for posting to the database string subscriberString = JsonConvert.SerializeObject(NewSubscriber); //Send a post request to the api with the invalid subscriber object response = await _client.PostAsync("api/Subscribers", new StringContent(subscriberString, Encoding.UTF8, "application/json")); //created status code is 400 - bad request since the object is invalid Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async void GetSubscriber_ValidRequest_AllSubscribersReturned() { AdminFixture.Load(_webApp.DBContext); //setting up login token var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PostAsync("api/Admins", StringContent); string adminToken = await response.Content.ReadAsStringAsync(); _client.SetBearerToken(adminToken); //_webApp.ReloadDBFixtures(); //SubscriberFixture.Reload(_webApp.DBContext); _webApp.Load(_webApp.DBContext); //The subscribers that are expected to be returned List <Subscriber> expectedSubscribers = _webApp.DBContext.Subscriber.ToList(); //Sends a get request to the API response = await _client.GetAsync("api/Subscribers"); //String we expect to get back (in JSON form) string responseJSON = await response.Content.ReadAsStringAsync(); //Deserailize the returned json string of subscribers into a list of subscriber objects List <Subscriber> actualSubscribers = JsonConvert.DeserializeObject <List <Subscriber> >(responseJSON); //Check subscriber acount returned to ensure the size is X //size is 111 instead of the original 10 since 100 subscriebrs are now added //for CSV file checking Assert.Equal(134, actualSubscribers.Count); //Break each Subscriber up in the actualSubcribers List //This assertColleciton will: //Ensure the subscriberID is an int //And check each attribute that was returned against our hard coded objects from our ficture for (int i = 0; i < actualSubscribers.Count; i++) { Assert.IsType <int>(actualSubscribers[i].subscriberID); Assert.Equal(expectedSubscribers[i].locationID, actualSubscribers[i].locationID); Assert.Equal(expectedSubscribers[i].isBusiness, actualSubscribers[i].isBusiness); //Assert.Equal(expectedCollections[i].description, actualCollections[i].description); } }
public async void UpdateSubscriber_InvalidSubscriber_SubscriberNotUpdateInDB() { AdminFixture.Load(_webApp.DBContext); //setting up login token var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PostAsync("api/Admins", StringContent); string adminToken = await response.Content.ReadAsStringAsync(); _client.SetBearerToken(adminToken); //Reload the database. //_webApp.ReloadDBFixtures(); //SubscriberFixture.Reload(_webApp.DBContext); _webApp.Load(_webApp.DBContext); //send a get request to the API to get all subscriber response = await _client.GetAsync("api/Subscribers"); string responseJSON = await response.Content.ReadAsStringAsync(); //deserialize all subscribers into a list of subscriber objects List <Subscriber> actualSubscribers = JsonConvert.DeserializeObject <List <Subscriber> >(responseJSON); //attempt to update the first subscriber in the lsit Subscriber UpdateSubscriber = actualSubscribers[0]; UpdateSubscriber.phoneNumber = "2222"; int id = UpdateSubscriber.subscriberID; //convert the subsciber to a string for posting to the database JsonMediaTypeFormatter format = new JsonMediaTypeFormatter(); var stringContent = new ObjectContent(UpdateSubscriber.GetType(), UpdateSubscriber, format); response = await _client.PutAsync("api/Subscribers/" + id, stringContent); //created status code is 400 bad request - since the object is invalid Assert.Equal(HttpStatusCode.BadRequest, response.StatusCode); }
public async void RoutesInfoReturnedInCSVFormat_RequestForLargeRoute_RouteInfoReturnedInCSVFormatAsync(string routeName) { AdminFixture.Load(_webApp.DBContext); //setting up login token var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json"); HttpResponseMessage response = await _client.PostAsync("api/Admins", StringContent); string adminToken = await response.Content.ReadAsStringAsync(); _client.SetBearerToken(adminToken); //reload the db _webApp.Load(_webApp.DBContext); //get the route that is expected to contain all of the locations and subscribers Route r = _webApp.DBContext.Route.Where(e => e.routeName.Equals(routeName)).FirstOrDefault(); //get all locations in the same region as this route List <Location> locations = _webApp.DBContext.Location.AsQueryable().Where(e => e.regionID == r.regionID).ToList <Location>(); //instantiate list of subscribers List <Subscriber> subscribers = new List <Subscriber>(); //for each location in the list get its associated subscriber only if the subscriber is currently active. Inactive subscribers will not have their locations be completed on this route. foreach (Location l in locations) { subscribers.Add(_webApp.DBContext.Subscriber.AsQueryable().Where(e => e.locationID == l.locationID).FirstOrDefault()); } //from each subscriber, create a new object with data from both the subscriber and their location as one single object var rows = subscribers.AsQueryable().Select(s => new { s.location.address, s.location.city, s.location.province, s.location.postalCode, s.firstName, s.lastName, s.phoneNumber, s.email }).ToList(); //create a string with headers for csv file of the expected outcome string expected = "Address,City,Province,Postal Code,First Name,Last Name,Phone Number,Email\n"; //for each object (subscriber-location) in the rows variable, add its data in csv format to the expected string. foreach (var row in rows) { expected += row.address + ',' + row.city + ',' + row.province + ',' + row.postalCode + ',' + row.firstName + ',' + row.lastName + ',' + row.phoneNumber + ',' + row.email + "\n"; } //get the CSV data back from the API in a response response = await _client.GetAsync("api/Routes/" + "export-r" + r.routeID); //parse the data into a json object and get the value of the content string responseString = JObject.Parse(response.Content.ReadAsStringAsync().Result).First.Next.First.ToString(); //ensure that the CSV content retrieved back from the API matches what our expected string Assert.Equal(expected, responseString); }
public async void AddSubscriber_ValidSubscriber_SubscriberAddedToDB() { //obtained from google string googleAuth = GoogleAuth.getGoogleAuth(_client); _client.DefaultRequestHeaders.Add("GoogleAuth", googleAuth); //Reload the database. //SubscriberFixture.Reload(_webApp.DBContext); _webApp.Load(_webApp.DBContext); //create a new subscriber to add to the database with valid fields //Subscriber NewSubscriber = DatabaseSubscriberFixture.GetDerivedSubscribers()[0]; Subscriber NewSubscriber = new Subscriber { firstName = "John", lastName = "Doe", email = "*****@*****.**", phoneNumber = "1234567890", isBusiness = false }; Location location = _webApp.DBContext.Location.First(); NewSubscriber.locationID = location.locationID; //convert the subsciber to a string for posting to the database StringContent stringContent = new StringContent(JsonConvert.SerializeObject(NewSubscriber), Encoding.UTF8, "application/json"); //send a post request to the api with the Subscriber object in JSON form var response = await _client.PostAsync("api/Subscribers", stringContent); //get the response from the api and read it into a string string responseString = await response.Content.ReadAsStringAsync(); //deserialize the response into a Subscriber object Subscriber deserializedSubscriber = JsonConvert.DeserializeObject <Subscriber>(responseString); //created status code is 201 Assert.Equal(HttpStatusCode.Created, response.StatusCode); AdminFixture.Load(_webApp.DBContext); //setting up login token var StringContent = new StringContent(JsonConvert.SerializeObject(admin), Encoding.UTF8, "application/json"); response = await _client.PostAsync("api/Admins", StringContent); string adminToken = await response.Content.ReadAsStringAsync(); _client.SetBearerToken(adminToken); //try to get the added subscriber form the database using the response data from earlier request. response = await _client.GetAsync("api/Subscribers/" + deserializedSubscriber.subscriberID); //read the response as a string responseString = await response.Content.ReadAsStringAsync(); //deserialize the object retrieved from the database Subscriber GetSubscriber = JsonConvert.DeserializeObject <Subscriber>(responseString); //check each field of the retrieved Subscriber and ensure it is equal to the subscriber that was initially send in the POST request Assert.IsType <int>(deserializedSubscriber.subscriberID); Assert.Equal(NewSubscriber.firstName, GetSubscriber.firstName); Assert.Equal(NewSubscriber.lastName, GetSubscriber.lastName); //Assert.Equal(NewSubscriber.Password, GetSubscriber.Password); Assert.Equal(NewSubscriber.email, GetSubscriber.email); Assert.Equal(NewSubscriber.phoneNumber, GetSubscriber.phoneNumber); Assert.Equal(NewSubscriber.isBusiness, GetSubscriber.isBusiness); Assert.IsType <int>(GetSubscriber.locationID); }