def setUp(self): """This method is the setup for add a user in the test and a request factory""" self.factory = RequestFactory() self.user = User.objects.create_user(pk=1, first_name='toto', last_name='titi', email='*****@*****.**', date_joined='2020-01-01', phone='01-01-01-01-02', password='******', date_of_birth='1995-12-03', postal_address='Totoland') self.new_announce = Announces(pk=1, title='TestAnnounce', address='image', description='test', city='test', country='test', price_day=100, price_weeks=150, email='*****@*****.**', pics_1='test', pics_2='test', pics_3='test', booking='', code=1000, user_id=1) self.new_announce.save() self.new_myrental = MyRental(pk=1, rental_city='test', rental_country='test', email_user_rental='*****@*****.**', code=1000, user_id=1) self.new_myrental.save() self.new_booking = Booking(pk=1, date_min='2020/01/01', date_max='2020/02/02', code=1000, user_id=1) self.new_booking.save()
def bookAgent(request): if request.method == 'POST': start = datetime.strptime(str(datetime.now().date()) + ':' + str(request.POST['start']), '%Y-%m-%d:%H:%M') end = datetime.strptime(str(datetime.now().date()) + ':' + str(request.POST['end']), '%Y-%m-%d:%H:%M') amount = getAmount(datetime.now(), end) get_balance = UserDetails.objects.filter(user_id=request.user).get() if get_balance.balance >= amount: agents = Agents.objects.filter(id=request.POST['agent']).get() if checkTime(start, end): book = Booking() book.agent = agents book.booked_by = request.user book.startTime = datetime.now() book.endTime = end book.paidStatus = False book.renew = False book.received = 0 book.total_amount = amount book.save() pay = debitUserAccount(request.user, amount) if pay: book.received = amount book.paidStatus = True book.save() agents.booked_status = True agents.save() booked = bookedAgents() booked.agent_id = agents booked.booked_id = book booked.save() messages.success(request, 'Booking Made Successfully') return redirect('home') else: messages.info(request, 'Make Sure That The Current Time And Booking Time Is Not More Than 25 min And Not Less Than 16 min') return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: messages.info(request, 'Insufficient Balance Total Amount Was' + str(amount) + 'Your Account Has' + str( get_balance.balance)) return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
class MyRentPageTestCase(TestCase): """This class is class for the tests of the MyRent dashboard""" def setUp(self): """This method is the setup for add a user in the test and a request factory""" self.factory = RequestFactory() self.user = User.objects.create_user(pk=1, first_name='toto', last_name='titi', email='*****@*****.**', date_joined='2020-01-01', phone='01-01-01-01-02', password='******', date_of_birth='1995-12-03', postal_address='Totoland') self.new_announce = Announces(pk=1, title='TestAnnounce', address='image', description='test', city='test', country='test', price_day=100, price_weeks=150, email='*****@*****.**', pics_1='test', pics_2='test', pics_3='test', booking='', code=1000, user_id=1) self.new_announce.save() self.new_myrental = MyRental(pk=1, rental_city='test', rental_country='test', email_user_rental='*****@*****.**', code=1000, user_id=1) self.new_myrental.save() self.new_booking = Booking(pk=1, date_min='2020/01/01', date_max='2020/02/02', code=1000, user_id=1) self.new_booking.save() def test_myRent_page_return_200_1(self): """MyRented access test with user connected""" print('Myrented access with user connected') request = self.factory.get(reverse('rented')) request.user = self.user response = rented(request) self.assertEqual(response.status_code, 200) def test_myRented_page_return_200_2(self): """Myrented access test with user not connected""" print('Myrented access test with user not connected') response = self.client.get(reverse('rented')) self.assertEqual(response.status_code, 200) def test_auto_delete_booking(self): """Test auto delete booking with user connected""" print('Test auto delete booking with user connected') request = self.factory.get(reverse('connect')) request.user = self.user auto_delete_booking(request)
class RentNowTestCase(TestCase): """This class is class for the tests of the MyRent dashboard""" def setUp(self): """This method is the setup for add a user in the test and a request factory""" self.factory = RequestFactory() self.user = User.objects.create_user(pk=1, first_name='toto', last_name='titi', email='*****@*****.**', date_joined='2020-01-01', phone='01-01-01-01-02', password='******', date_of_birth='1995-12-03', postal_address='Totoland') self.new_announce = Announces(pk=1, title='TestAnnounce', address='image', description='test', city='test', country='test', price_day=100, price_weeks=150, email='*****@*****.**', pics_1='test', pics_2='test', pics_3='test', booking='', code=1000, user_id=1) self.new_announce.save() self.new_myrental = MyRental(pk=1, rental_city='test', rental_country='test', email_user_rental='*****@*****.**', code=1000, user_id=1) self.new_myrental.save() self.new_booking = Booking(pk=1, date_min='2020/01/01', date_max='2020/02/02', code=1000, user_id=1) self.new_booking.save() def test_rent_now_page_return_200_1(self): """Test access rent now with user connected""" print('Test access rent now with user connected') request = self.factory.get(reverse('rent_now'), {'announce': 1000}) request.user = self.user response = rent_now(request) self.assertEqual(response.status_code, 200) def test_rent_now_page_return_200_2(self): """Test access rent now with user not connected""" print('Test access rent now with user not connected') response = self.client.get(reverse('rent_now'), {'announce': 1000}) self.assertEqual(response.status_code, 200) def test_rent_validation_page_return_200(self): """This test is for test the validation of the rent booking""" print('Test the validation of the rent booking') request = self.factory.get(reverse('rent_validation'), {'announce': 1000}) request.user = self.user announce_id = request.GET.get('announce') announce = Announces.objects.filter(code=announce_id) for info in announce: amount = info.price_day * 1 user = User.objects.get(pk=info.user_id) response = payment_process(request, info, user.email_paypal, amount) self.assertEqual(response.status_code, 200) def test_check_the_available_dates(self): """Test check the available dates""" print('Test check the available dates') request = self.factory.get(reverse('check_dates'), {'announce': 1000}) request.user = self.user response = check_the_available_dates(request) self.assertEqual(response.status_code, 200)
class HandlerAdPageTestCase(TestCase): """This class is class for the tests of the handler""" def setUp(self): """This method is the setup for add a user in the test and a request factory""" self.factory = RequestFactory() self.user = User.objects.create_user(pk=1, first_name='toto', last_name='titi', email='*****@*****.**', date_joined='2020-01-01', phone='01-01-01-01-02', password='******', date_of_birth='1995-12-03', postal_address='Totoland') self.new_announce = Announces(pk=1, title='TestAnnounce', address='image', description='test', city='test', country='test', price_day=100, price_weeks=150, email='*****@*****.**', pics_1='test', pics_2='test', pics_3='test', booking='', code=1000, user_id=1) self.new_announce.save() self.new_myrental = MyRental(pk=1, rental_city='test', rental_country='test', email_user_rental='*****@*****.**', code=1000, user_id=1) self.new_myrental.save() self.new_booking = Booking(pk=1, date_min='2020/05/05', date_max='2020/05/05', code=1000, user_id=1) self.new_booking.save() def test_handlerAd_page_return_200_1(self): """Handler test not connected""" print("Handler test not connected") response = self.client.get(reverse('handler_ad')) self.assertEqual(response.status_code, 200) def test_handlerAd_page_return_200_2(self): """Handler test connected""" print('Handler test connected') request = self.factory.get(reverse('handler_ad')) request.user = self.user response = dashboard(request) self.assertEqual(response.status_code, 200) def test_changePrice_page_return_200(self): """Change Price test""" print('Change Price test') request = self.factory.get(reverse('change_price'), {'change': 1000}) code_change = request.GET.get('change') announce = Announces.objects.filter(code=code_change) price_day = 100 price_weeks = 150 request.user = self.user response = change_price_validation(request, announce, price_day, price_weeks) self.assertEqual(response.status_code, 200) def test_changePrice_page_return_error(self): """Change Price test in change_price page""" print('Change Price test in change_price page') try: request = self.factory.get(reverse('change_price'), {'change': 1000}) user = User.objects.get(pk=1) request.user = self.user middleware = SessionMiddleware() middleware.process_request(request) request.session['member_id'] = user.id request.session.save() response = change_price(request) self.assertEqual(response.status_code, 200) # This except return a pass because the number in the post is None except AttributeError: pass def test_changePrice_page_return_error_200(self): """Change Price test with str""" print('Change Price test with str') self.client.post( reverse('sign_up'), { 'email': '*****@*****.**', 'password': '******', 'confirmation_password': '******', 'last_name': 'name', 'first_name': 'surname', 'phone': '02-01-02-01-02', 'date_of_birth': '19/02/1995', 'postal_address': 'address', 'gcu': True }) response = self.client.post(reverse('change_price'), { 'price_day': str(100), 'price_weeks': str(100) }) # Error is 200 because this error is managed directly by the code self.assertEqual(response.status_code, 200) def test_changePriceAnnounce_page_return_200(self): """Change Price test with announce in URL""" print('Change Price test with announce in URL') self.client.post( reverse('sign_up'), { 'email': '*****@*****.**', 'password': '******', 'confirmation_password': '******', 'last_name': 'name', 'first_name': 'surname', 'phone': '02-01-02-01-02', 'date_of_birth': '19/02/1995', 'postal_address': 'address', 'gcu': True }) response = self.client.get(reverse('change_price'), {'announce': 1000}) self.assertEqual(response.status_code, 200) def test_changePriceNotConnected_page_return_200(self): """Change Price test with user not connected""" print('Change Price test with user not connected') response = self.client.get(reverse('change_price'), {'announce': 1000}) self.assertEqual(response.status_code, 200) def test_changePics_page_return_200(self): """Change Price test""" print('Change Pics test') self.client.post( reverse('sign_up'), { 'email': '*****@*****.**', 'password': '******', 'confirmation_password': '******', 'last_name': 'name', 'first_name': 'surname', 'phone': '02-01-02-01-02', 'date_of_birth': '19/02/1995', 'postal_address': 'address', 'gcu': True }) response = self.client.post(reverse('change_pics'), { 'pics_1': 'test.jpg', 'pics_2': 'test.jpg', 'pics_3': 'test.jpg' }) self.assertEqual(response.status_code, 200) def test_changePicsNotConnected_page_return_200(self): """Change Pics test with user not connected""" print('Change Pics test with user not connected') response = self.client.get(reverse('change_pics'), {'announce': 1000}) self.assertEqual(response.status_code, 200) def test_changePicsAnnounce_page_return_200(self): """Change Pics test with announce in URL""" print('Change Price test with announce in URL') request = self.factory.get(reverse('change_pics'), {'announce': 1000}) request.user = self.user response = change_pics(request) self.assertEqual(response.status_code, 200) def test_cancelRented_page_return_200_1(self): """Cancel rented test with user connected""" print('Cancel rented test with user connected') request = self.factory.get(reverse('delete_rent'), {'cancel': 1000}) request.user = self.user code_cancel = request.GET.get('cancel') if 'cancel' in request.GET: announce = Announces.objects.filter(code=code_cancel) response = cancel_myrented(request, announce) self.assertEqual(response.status_code, 200) def test_cancelRented_page_return_200_2(self): """Cancel rented test with user not connected""" print('Cancel rented test with user not connected') response = self.client.get(reverse('cancel_rented'), {'announce': 1000}) self.assertEqual(response.status_code, 200) def test_cancelRentedError_page_return_200(self): """Cancel rented Error test with user not connected""" print('Cancel rented Error test with user not connected') response = self.client.get(reverse('delete_rent'), {'': ''}) self.assertEqual(response.status_code, 200) def test_deleteRentedAnnounce_page_return_200(self): """Delete Rented test with user connected and announce""" print('Delete Rentedtest with user connected and announce') self.client.post( reverse('sign_up'), { 'email': '*****@*****.**', 'password': '******', 'confirmation_password': '******', 'last_name': 'name', 'first_name': 'surname', 'phone': '02-01-02-01-02', 'date_of_birth': '19/02/1995', 'postal_address': 'address', 'gcu': True }) response = self.client.get(reverse('delete_rent'), {'announce': 1000}) self.assertEqual(response.status_code, 200) def test_deleteRentedCancel_page_return_200_1(self): """Delete rented test with user not connected and cancel""" print('Delete rented test with user connected and cancel') request = self.factory.get(reverse('delete_rent'), {'cancel': 1000}) request.user = self.user code_cancel = request.GET.get('cancel') if 'cancel' in request.GET: announce = Announces.objects.filter(code=code_cancel) response = delete_my_offer(request, announce) self.assertEqual(response.status_code, 200) def test_deleteRentedCancel_page_return_200_2(self): """Delete rented test with user not connected and cancel""" print('Delete rented test with user not connected' ' and cancel') response = self.client.get(reverse('delete_rent'), {'cancel': 1000}) self.assertEqual(response.status_code, 200) def test_deleteRentedNone_page_return_200(self): """Delete MyOffer test with user connected and None""" print('Delete MyOffer test with user connected' ' and None') self.client.post( reverse('sign_up'), { 'email': '*****@*****.**', 'password': '******', 'confirmation_password': '******', 'last_name': 'name', 'first_name': 'surname', 'phone': '02-01-02-01-02', 'date_of_birth': '19/02/1995', 'postal_address': 'address', 'gcu': True }) response = self.client.get(reverse('delete_rent'), {'None': ''}) self.assertEqual(response.status_code, 200)
class PaymentProcessDonePageTestCase(TestCase): """This class is class for the tests of the payment process done""" def setUp(self): """This method is the setup for add a user in the test and a request factory""" self.factory = RequestFactory() self.user = User.objects.create_user(pk=1, first_name='toto', last_name='titi', email='*****@*****.**', date_joined='2020-01-01', phone='01-01-01-01-02', password='******', date_of_birth='1995-12-03', postal_address='Totoland') self.new_announce = Announces(pk=10, title='TestAnnounce', address='image', description='test', city='test', country='test', price_day=100, price_weeks=150, email='*****@*****.**', pics_1='test', pics_2='test', pics_3='test', booking='', code=1000, user_id=1) self.new_announce.save() self.new_myrental = MyRental(pk=10, rental_city='test', rental_country='test', email_user_rental='*****@*****.**', code=1000, user_id=1) self.new_myrental.save() self.new_booking = Booking(pk=10, date_min='2020/05/05', date_max='2020/05/05', code=1000, user_id=1) self.new_booking.save() def test_save_new_booking(self): """Test the new save in the booking table""" print('Test the new save in the booking table') request = self.factory.get(reverse('payment_done')) user = User.objects.get(pk=1) request.user = self.user announce = Announces.objects.filter(code=1000) middleware = SessionMiddleware() middleware.process_request(request) request.session['member_id'] = user.id request.session.save() d_min = '2020/05/05' d_max = '2020/05/05' get_info_booking(request, announce, d_min, d_max) rent_validation_done(request)