def test_garbageday_views_garbageday_edit_post_wrong_user(self): print('Testing garbageday.views.garbageday_edit() POST wrong user') self.client.force_login(self.user2) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-04" self.garbage_day.next_garbage_day = "2019-11-04" self.garbage_day.save() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_edit', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'main/404.html') self.assertNotContains(response, 'Edit Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertContains(response, '404') self.assertNotContains(response, 'Login') self.assertEqual(self.house.garbageday_set.first().last_garbage_day, datetime.date(2019, 11, 4)) self.assertEqual(self.house.garbageday_set.first().next_garbage_day, datetime.date(2019, 11, 4))
def test_garbageday_views_garbageday_create_post_existing(self): print('Testing garbageday.views.garbageday_create() POST existing') self.client.force_login(self.user) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-12" self.garbage_day.next_garbage_day = "2019-11-26" self.garbage_day.save() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_create', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'houses/house_detail.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login')
class GarbageDayViewTests(TestCase): def setUp(self): User = get_user_model() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.user2 = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.assertEqual(self.user.email, '*****@*****.**') self.assertEqual(self.user.username, 'Fred_Flintstone') self.assertTrue(self.user.is_active) self.assertFalse(self.user.is_staff) self.assertFalse(self.user.is_superuser) house = House.objects.create(user=self.user) house.place_id = 'EiwyNTI5IFN0YWxsaW9uIERyLCBPc2hhd2EsIE9OIEwxSCA3SzQsIENhbmFkYSIxEi8KFAoSCY_JD3vDG9WJEe3JFhlBvwOKEOETKhQKEgnrS9FlwxvViRHYx20MM9m-8g' house.lat = '43.95858010000001' house.lon = '-78.91587470000002' house.street_number = 2529 house.street_name = 'Stallion Drive' house.city = 'Oshawa' house.prov_state = 'ON' house.postal_code = 'L1H 0M4' house.country = 'Canada' house.save() self.house = house self.assertEquals(self.house.garbageday_set.count(), 0) def test_garbageday_utils_update_garbage_days_no_updated_needed(self): print( 'Testing garbageday.utils.update_garbage_days() No Update Needed') self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = datetime.date.today() self.garbage_day.next_garbage_day = datetime.date.today( ) + datetime.timedelta(days=14) print(self.garbage_day.last_garbage_day) print(self.garbage_day.next_garbage_day) self.garbage_day.save() print(self.garbage_day.last_updated) self.assertEquals(self.house.garbageday_set.count(), 1) self.assertEqual(self.house.garbageday_set.first().garbage_frequency, datetime.timedelta(days=14))
def test_garbage_day_house_relation(self): print('Testing GarbageDay House Relation') garbage_day = GarbageDay() garbage_day.house = self.house garbage_day.user = self.user garbage_day.last_garbage_day = "2019-11-12" garbage_day.next_garbage_day = "2019-11-26" garbage_day.save() self.assertIsNot(self.house.garbageday_set.count(), 0)
def test_garbageday_utils_update_garbage_days_no_updated_needed(self): print( 'Testing garbageday.utils.update_garbage_days() No Update Needed') self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = datetime.date.today() self.garbage_day.next_garbage_day = datetime.date.today( ) + datetime.timedelta(days=14) print(self.garbage_day.last_garbage_day) print(self.garbage_day.next_garbage_day) self.garbage_day.save() print(self.garbage_day.last_updated) self.assertEquals(self.house.garbageday_set.count(), 1) self.assertEqual(self.house.garbageday_set.first().garbage_frequency, datetime.timedelta(days=14))
def house_create(request): captcha = Captcha() GOOGLE_API_KEY = settings.GOOGLE_API_KEY if request.method == 'POST': house = House() house.user = request.user if ('street_number' or 'street_number' or 'street_name' or 'city' or 'prov_state' or 'country' or 'place_id' or 'lat' or 'lon') not in request.POST: return render(request, 'houses/house_create.html', {'error': 'There is an issue with the address inputted!', 'GOOGLE_API_KEY': GOOGLE_API_KEY, 'captcha': captcha}) else: # Create House house.street_number = request.POST['street_number'] house.street_name = request.POST['street_name'] house.city = request.POST['city'] house.prov_state = request.POST['prov_state'] house.postal_code = request.POST['postal_code'] house.country = request.POST['country'] house.place_id = request.POST['place_id'] house.lat = request.POST['lat'] house.lon = request.POST['lon'] house.save() # Create first and empty BillSet for house init_bill_set = BillSet() date = now() init_bill_set.month = date.month init_bill_set.year = date.year init_bill_set.house = house init_bill_set.save() # TODO: Put these in a background task or make them async as they take 90% of the request time # TODO: This can be remove at a later time, but at the moment it costs too much to test it constantly if 'test' in sys.argv: pass else: house.load_walk_score() house_image = load_house_image(house) # Create GarbageDay for house if info present if ('LastGarbageDay' or 'NextGarbageDay') not in request.POST: pass elif (request.POST['LastGarbageDay'] or request.POST['NextGarbageDay']) is '': pass else: garbageday = GarbageDay() garbageday.house = house garbageday.user = request.user garbageday.last_garbage_day = request.POST['LastGarbageDay'] garbageday.next_garbage_day = request.POST['NextGarbageDay'] garbageday.save() return redirect('house_detail', pk=house.id) else: return render(request, 'houses/house_create.html', {'GOOGLE_API_KEY': GOOGLE_API_KEY, 'captcha': captcha})
def test_create_garbage_day2(self): print('Testing GarbageDay Creation Filled') num_pre = GarbageDay.objects.count() garbage_day = GarbageDay() garbage_day.house = self.house garbage_day.user = self.user garbage_day.last_garbage_day = "2019-11-12" garbage_day.next_garbage_day = "2019-11-26" garbage_day.save() self.assertEqual(garbage_day.house, self.house) num_post = GarbageDay.objects.count() self.assertEqual(num_post, num_pre + 1) self.assertEqual(garbage_day.garbage_frequency, timedelta(weeks=2, days=0))
def test_garbageday_views_garbageday_manage_get_existing(self): print('Testing garbageday.views.garbageday_manage() GET existing') self.client.force_login(self.user) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-12" self.garbage_day.next_garbage_day = "2019-11-26" self.garbage_day.save() count = GarbageDay.objects.filter(house=self.house).count() self.assertEqual(count, 1) response = self.client.get(reverse('garbageday_manage', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'garbageday/garbageday_edit.html') self.assertContains(response, 'Edit Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login')
def garbageday_create(request, house): housemodel = get_object_or_404(House, pk=house) if request.user != housemodel.user: raise Http404 if housemodel.garbageday_set.count() > 0: return redirect(housemodel.get_absolute_url()) if request.method == 'POST': garbageday = GarbageDay() garbageday.user = request.user garbageday.house = housemodel garbageday.last_garbage_day = request.POST['LastGarbageDay'] garbageday.next_garbage_day = request.POST['NextGarbageDay'] garbageday.save() return redirect(housemodel.get_absolute_url()) else: return render(request, 'garbageday/garbageday_create.html', {'house': housemodel})
class GarbageDayViewTests(TestCase): def setUp(self): User = get_user_model() self.user = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.user2 = User.objects.create_user(username='******', email='*****@*****.**', password='******') self.assertEqual(self.user.email, '*****@*****.**') self.assertEqual(self.user.username, 'Fred_Flintstone') self.assertTrue(self.user.is_active) self.assertFalse(self.user.is_staff) self.assertFalse(self.user.is_superuser) house = House.objects.create(user=self.user) house.place_id = 'EiwyNTI5IFN0YWxsaW9uIERyLCBPc2hhd2EsIE9OIEwxSCA3SzQsIENhbmFkYSIxEi8KFAoSCY_JD3vDG9WJEe3JFhlBvwOKEOETKhQKEgnrS9FlwxvViRHYx20MM9m-8g' house.lat = '43.95858010000001' house.lon = '-78.91587470000002' house.street_number = 2529 house.street_name = 'Stallion Drive' house.city = 'Oshawa' house.prov_state = 'ON' house.postal_code = 'L1H 0M4' house.country = 'Canada' house.save() self.house = house def test_garbageday_views_garbageday_manage_get_not_existing(self): print('Testing garbageday.views.garbageday_manage() GET not existing') self.client.force_login(self.user) count = GarbageDay.objects.filter(house=self.house).count() self.assertEqual(count, 0) response = self.client.get(reverse('garbageday_manage', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'garbageday/garbageday_create.html') self.assertContains(response, 'Setup Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_manage_get_existing(self): print('Testing garbageday.views.garbageday_manage() GET existing') self.client.force_login(self.user) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-12" self.garbage_day.next_garbage_day = "2019-11-26" self.garbage_day.save() count = GarbageDay.objects.filter(house=self.house).count() self.assertEqual(count, 1) response = self.client.get(reverse('garbageday_manage', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'garbageday/garbageday_edit.html') self.assertContains(response, 'Edit Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_manage_get_not_logged_in(self): print('Testing garbageday.views.garbageday_manage() GET not logged in') self.client.logout() response = self.client.get(reverse('garbageday_manage', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertContains(response, 'Login') def test_garbageday_views_garbageday_manage_get_wrong_user(self): print('Testing garbageday.views.garbageday_manage() GET wrong user') self.client.force_login(self.user2) response = self.client.get(reverse('garbageday_manage', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'main/404.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_create_get(self): print('Testing garbageday.views.garbageday_create() GET') self.client.force_login(self.user) response = self.client.get(reverse('garbageday_create', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'garbageday/garbageday_create.html') self.assertContains(response, 'Setup Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_create_get_not_logged_in(self): print('Testing garbageday.views.garbageday_create() GET not logged in') self.client.logout() response = self.client.get(reverse('garbageday_create', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertContains(response, 'Login') def test_garbageday_views_garbageday_create_get_wrong_user(self): print('Testing garbageday.views.garbageday_create() GET wrong user') self.client.force_login(self.user2) response = self.client.get(reverse('garbageday_create', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'main/404.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_create_post(self): print('Testing garbageday.views.garbageday_create() POST') self.client.force_login(self.user) pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_create', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertGreater(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'houses/house_detail.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_create_post_existing(self): print('Testing garbageday.views.garbageday_create() POST existing') self.client.force_login(self.user) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-12" self.garbage_day.next_garbage_day = "2019-11-26" self.garbage_day.save() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_create', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'houses/house_detail.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_create_post_not_logged_in(self): print( 'Testing garbageday.views.garbageday_create() POST not logged in') self.client.logout() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_create', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertContains(response, 'Login') def test_garbageday_views_garbageday_create_post_wrong_user(self): print('Testing garbageday.views.garbageday_create() POST wrong user') self.client.force_login(self.user2) pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_create', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'main/404.html') self.assertNotContains(response, 'Setup Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_edit_get(self): print('Testing garbageday.views.garbageday_edit() GET') self.client.force_login(self.user) response = self.client.get(reverse('garbageday_edit', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'garbageday/garbageday_edit.html') self.assertContains(response, 'Edit Garbage Day for') self.assertContains(response, self.house) self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_edit_get_not_logged_in(self): print('Testing garbageday.views.garbageday_edit() GET not logged in') self.client.logout() response = self.client.get(reverse('garbageday_edit', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertNotContains(response, 'Edit Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, '404') self.assertContains(response, 'Login') def test_garbageday_views_garbageday_edit_get_wrong_user(self): print('Testing garbageday.views.garbageday_edit() GET wrong user') self.client.force_login(self.user2) response = self.client.get(reverse('garbageday_edit', kwargs={'house': self.house.id}), follow=True) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'main/404.html') self.assertNotContains(response, 'Edit Garbage Day for') self.assertNotContains(response, self.house) self.assertContains(response, '404') self.assertNotContains(response, 'Login') def test_garbageday_views_garbageday_edit_post(self): print('Testing garbageday.views.garbageday_edit() POST') self.client.force_login(self.user) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-04" self.garbage_day.next_garbage_day = "2019-11-04" self.garbage_day.save() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_edit', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'houses/house_detail.html') self.assertNotContains(response, 'Edit Garbage Day for') self.assertContains(response, self.house) self.assertContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertNotContains(response, 'Login') self.assertEqual(self.house.garbageday_set.first().last_garbage_day, datetime.date(2019, 11, 12)) self.assertEqual(self.house.garbageday_set.first().next_garbage_day, datetime.date(2019, 11, 26)) def test_garbageday_views_garbageday_edit_post_not_logged_in(self): print('Testing garbageday.views.garbageday_edit() POST not logged in') self.client.logout() self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-04" self.garbage_day.next_garbage_day = "2019-11-04" self.garbage_day.save() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_edit', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'account/login.html') self.assertNotContains(response, 'Edit Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertNotContains(response, '404') self.assertContains(response, 'Login') self.assertEqual(self.house.garbageday_set.first().last_garbage_day, datetime.date(2019, 11, 4)) self.assertEqual(self.house.garbageday_set.first().next_garbage_day, datetime.date(2019, 11, 4)) def test_garbageday_views_garbageday_edit_post_wrong_user(self): print('Testing garbageday.views.garbageday_edit() POST wrong user') self.client.force_login(self.user2) self.garbage_day = GarbageDay() self.garbage_day.house = self.house self.garbage_day.user = self.user self.garbage_day.last_garbage_day = "2019-11-04" self.garbage_day.next_garbage_day = "2019-11-04" self.garbage_day.save() pre_count = GarbageDay.objects.count() req_data = { 'LastGarbageDay': '2019-11-12', 'NextGarbageDay': '2019-11-26' } response = self.client.post(reverse('garbageday_edit', kwargs={'house': self.house.id}), req_data, follow=True) post_count = GarbageDay.objects.count() self.assertEqual(post_count, pre_count) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'main/404.html') self.assertNotContains(response, 'Edit Garbage Day for') self.assertNotContains(response, self.house) self.assertNotContains(response, 'Garbage Day') self.assertContains(response, '404') self.assertNotContains(response, 'Login') self.assertEqual(self.house.garbageday_set.first().last_garbage_day, datetime.date(2019, 11, 4)) self.assertEqual(self.house.garbageday_set.first().next_garbage_day, datetime.date(2019, 11, 4))