class CancelOrderReasonTestCase(BaseTestCase): """ Tests the functionality for submitting a cancel request reason """ def setUp(self): """ Initialize testing environment. """ super(CancelOrderReasonTestCase, self).setUp() User.objects.create(email="*****@*****.**", password="******") self.uriel = Client() user = User.objects.get(email="*****@*****.**") user.is_active = True user.save() self.uriel.force_login(user) def test_submit_reason_success(self): """ Test user can successfuly submit a reason. """ data = {'other_reason': 'I dont like the color.'} response = self.uriel.post('/submit_reason', data, follow=True) self.assertRedirects(response, '/', 302) def test_submit_form_with_error(self): """ Test application does not allow form with error. """ data = {'other_reason': '$$$$$$$$$$$$'} response = self.uriel.post('/submit_reason', data) self.assertEqual(response.request['PATH_INFO'], '/submit_reason')
class LoginViewTest(BaseTestCase): def setUp(self): super(LoginViewTest, self).setUp() self.bed_rock = Client() User.objects.create(email="*****@*****.**", first_name="timon", last_name="pumba", is_staff=True, is_active=True, is_change_allowed=False, country_code=self.code, phone_number=722000000) self.timon = User.objects.get(email="*****@*****.**") self.timon.set_password("secret") self.timon.save() def test_next_on_login(self): """ Test that if a user visits the login page with the next on the variable on the URL - That a successful login redirects to the URL specified on the next variable """ login_data = {"email": "*****@*****.**", "password": "******"} response = self.bed_rock.post("/login?next=/dashboard", login_data) self.assertRedirects(response, "/dashboard")
def test_order_data_post_request(self): ''' Test that if a user moves to the dashboard, and that he had made some orders and he posts something: - That the order details for his past orders are rendered: ''' winniethepooh = Client() User.objects.create(email="*****@*****.**") user = User.objects.get(email="*****@*****.**") cart = Cart.objects.create( phone_model_item=self.samsung_note_5_rose_gold, quantity=3, owner=user) data = { 'hidden_pickup': 1, 'country_code': self.code.id, 'phone_number': '0715557775', 'location': 'Rongai, Posta' } winniethepooh.force_login(user) post_response = winniethepooh.post('/order', data, follow=True) order = Order.objects.get(owner=user) self.assertContains(post_response, cart.phone_model_item) self.assertContains( post_response, "<p><b>Order No:</b><span> #{}</span>".format(order.pk)) self.assertContains( post_response, "<b>Payment Method:</b><span> {}</span>".format( order.payment_method)) self.assertContains( post_response, "<b>Quantity:</b><span> {}</span>".format(order.quantity)) self.assertContains( post_response, "<b>Total Price:</b><span> {}</span>".format( "{:,}".format(order.total_price))) self.assertContains(post_response, "<span> {}</span>".format(order.status)) order_date = order.date nairobi_time_zone = pytz.timezone('Africa/Nairobi') fmt = "%b %d %Y %H:%M" utc = order_date.replace(tzinfo=pytz.UTC) localtz = utc.astimezone(nairobi_time_zone) localtz = localtz.strftime(fmt) purchase_date = "<b>Purchase Date: </b><span>{} : EAT</span>".\ format(localtz) self.assertContains(post_response, purchase_date) self.assertContains(post_response, "<li>Recipient: {}</li>".format(user)) self.assertContains( post_response, "Location: {}".format(order.shipping_address.location)) self.assertContains( post_response, "Pick up: {}".format(order.shipping_address.location)) self.assertContains( post_response, "<li>Phone No: {}</li>".format( order.shipping_address.phone_number))
def test_shipping_address_created(self): """ Test that when the user places an order and the shipping address information is sent: - Their shipping data is saved """ winniethepooh = Client() User.objects.create(email="*****@*****.**") order_user = User.objects.get(email="*****@*****.**") Cart.objects.create(phone_model_item=self.samsung_note_5_rose_gold, quantity=3, owner=order_user) data = { 'hidden_pickup': 1, 'country_code': self.code.id, 'phone_number': '0715557775', 'location': 'Rongai, Posta' } winniethepooh.force_login(order_user) post_response = winniethepooh.post('/order', data, follow=True) self.assertRedirects(post_response, '/dashboard#orders', 302)
def test_order_to_pickup(self): """ Test that a user can order to pickup from company premises """ winniethepooh = Client() User.objects.create(email="*****@*****.**") user = User.objects.get(email="*****@*****.**") cart = Cart.objects.create( phone_model_item=self.samsung_note_5_rose_gold, quantity=3, owner=user) data = {} winniethepooh.force_login(user) post_response = winniethepooh.post('/order', data, follow=True) order = Order.objects.get(owner=user) self.assertContains(post_response, cart.phone_model_item) self.assertContains( post_response, "<p><b>Order No:</b><span> #{}</span>".format(order.pk)) self.assertContains(post_response, "<li>Recipient: {}</li>".format(user)) self.assertContains(post_response, "Pick up: To Pick up")
def test_shipping_address_form_validation_failure(self): """ Test that when the user places an order and the shipping address information sent is incorrect: - They are notified of the error """ winniethepooh = Client() User.objects.create(email="*****@*****.**") order_user = User.objects.get(email="*****@*****.**") Cart.objects.create(phone_model_item=self.samsung_note_5_rose_gold, quantity=3, owner=order_user) data = { 'hidden_pickup': 1, 'country_code': self.code.id, 'phone_number': '071555777578899', 'location': 'Rongai, Posta' } winniethepooh.force_login(order_user) post_response = winniethepooh.post('/order', data) self.assertContains(post_response, "The phone number entered is invalid.")
class ChangeEmailView(BaseTestCase): """Tests functionality for changing email.""" def setUp(self): """Set up initial state of tests.""" super(ChangeEmailView, self).setUp() def test_not_logged_in(self): ''' Test that when a user is not logged in: - That he is redirected to a log in page ''' User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.sivanna = Client() response = self.sivanna.get("/change_email") self.assertRedirects(response, "/login?next=/change_email", 302) def test_change_allowed_false(self): ''' Test that when a user is logged in and change allowed is False: - That the user is redirected to the confirm_user page ''' User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.sivanna = Client() self.sivanna.force_login(self.user) response = self.sivanna.get("/change_email") self.assertRedirects(response, "/confirm_user", 302) def test_logged_in_change_allowed_true(self): ''' Test that when a user is logged in and that change allowed is True: - That the user can view the page ''' User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.user.is_change_allowed = True self.user.save() self.sivanna = Client() self.sivanna.force_login(self.user) response = self.sivanna.get("/change_email") self.assertEqual(response.status_code, 200) def test_wrong_email_format(self): ''' Test that when an invalid email format is provided: - That an error is raised ''' User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.user.is_change_allowed = True self.user.save() self.sivanna = Client() self.sivanna.force_login(self.user) response = self.sivanna.post("/change_email", {"email": "pop@gmail"}) self.assertEqual(response.status_code, 200) self.assertContains(response, EmailValidator.message) def test_existent_email(self): ''' Test that when an existent email is provided: - That an error is raised ''' # create another user with some credentials for testing User.objects.create_user(email="*****@*****.**", password="******") User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.user.is_change_allowed = True self.user.save() self.sivanna = Client() self.sivanna.force_login(self.user) response = self.sivanna.post("/change_email", {"email": "*****@*****.**"}) self.assertEqual(response.status_code, 200) error = "The email address you entered has already been registered." self.assertContains(response, "{}".format(error)) message = "Please provide new email for [email protected]" self.assertContains(response, message) def test_similar_email(self): ''' Test that when a similar email to the user's original email is provided as a candidate for change: - That an error is raised ''' User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.user.is_change_allowed = True self.user.save() self.sivanna = Client() self.sivanna.force_login(self.user) response = self.sivanna.post("/change_email", {"email": "*****@*****.**"}) self.assertEqual(response.status_code, 200) error_message = ChangeEmailForm.error_messages['invalid_email'] self.assertContains(response, error_message) message = "Please provide new email for [email protected]" self.assertContains(response, message) def test_valid_data(self): ''' Test that when a valid email is provided that: - The users change_allowed attribute is set to False, change_email is set to the intended email, change_email_tracker to a time - That the status code is 200 - That a html with information is rendered ''' User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.user.is_change_allowed = True self.user.save() self.sivanna = Client() self.sivanna.force_login(self.user) self.assertIsNone(self.user.change_email_tracker) response = self.sivanna.post("/change_email", {"email": "*****@*****.**"}) self.assertEqual(response.status_code, 200) edited_user = User.objects.get(email="*****@*****.**") self.assertFalse(edited_user.is_change_allowed) self.assertEqual(edited_user.change_email, "*****@*****.**") self.assertIsNotNone(edited_user.change_email_tracker) msg = "We have sent a link to your new email: "\ "<b>[email protected]</b>" self.assertContains(response, "{}".format(msg))
class ConfirmUserView(BaseTestCase): """Tests user confirmation view.""" def setUp(self): User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.sivanna = Client() super(ConfirmUserView, self).setUp() def test_login_required_for_confirm_user(self): ''' Test that when a user is not logged in: - That he or she cannot access the confirm_user page link Test that when a user is logged in: - That he can access the confirm_user page link ''' response = self.sivanna.get("/confirm_user") self.assertRedirects(response, "/login?next=/confirm_user", 302) def test_logged_in_access_confirm_user(self): ''' Test that when a user is logged in: - That he can access the confirm_user page link ''' self.sivanna.force_login(self.user) response = self.sivanna.get("/confirm_user") self.assertEqual(response.status_code, 200) def test_wrong_email_format(self): ''' Test that when a wrong email format is granted: - That the page does not redirect. - That an error message is raised. ''' self.sivanna.force_login(self.user) response = self.sivanna.post("/confirm_user", { "email": "pop@gmail", "password": "******" }) self.assertEqual(response.status_code, 200) self.assertContains(response, EmailValidator.message) def test_non_existent_email(self): ''' Test that when a valid email format is entered but it does not exist that - The page does not redirect. - An error message is raised ''' self.sivanna.force_login(self.user) response = self.sivanna.post("/confirm_user", { "email": "*****@*****.**", "password": "******" }) self.assertEqual(response.status_code, 200) error_message = AuthenticationForm().error_messages['invalid_email'] self.assertContains(response, error_message) def test_wrong_password(self): ''' Test that when a valid email is entered with a wrong password that - The page does not redirect. - An error message is raised. ''' self.sivanna.force_login(self.user) response = self.sivanna.post("/confirm_user", { "email": "*****@*****.**", "password": "******" }) self.assertEqual(response.status_code, 200) error_message = AuthenticationForm().error_messages['invalid_login'] self.assertContains(response, error_message) def test_valid_credentials(self): ''' Test that when valid credentials are granted to the view: - That the page redirects - The the user's attribute 'is_change_allowed' changes to true ''' self.sivanna.force_login(self.user) response = self.sivanna.post("/confirm_user", { "email": "*****@*****.**", "password": "******" }) self.assertRedirects(response, "/change_email", 302) user = User.objects.get(email="*****@*****.**") self.assertEqual(user.is_change_allowed, True)
class LogoutViewTest(BaseTestCase): def setUp(self): super(LogoutViewTest, self).setUp() self.flinstones = Client() user = User.objects.create_user( email='*****@*****.**', password='******', first_name='flinstones' ) self.flinstones.force_login(user) def test_logout_user(self): """ Test that if a user has logged into a site and the user logs out - That his status changes to unauthenticated. """ after_login_response = self.flinstones.get("/") self.assertTrue(after_login_response.context["user"].is_authenticated) self.flinstones.post("/logout") after_logout_response = self.flinstones.get("/") self.assertFalse( after_logout_response.context["user"].is_authenticated) def test_redirect_to_current_page_with_referer_header(self): """ Test that if a user has logged out of any page on a browser that submits the HTTP_REFERER header - That he is redirected to the page he is on. """ logout_response = self.flinstones.post("/logout", HTTP_REFERER="/help") self.assertRedirects(logout_response, "/help", 302) def test_redirect_to_home_page_without_referer_header(self): """ Test that if a user has logged out of any page on a browser that does not submit the HTTP_REFERER header - That he is redirected to the home page. """ get_help_response = self.flinstones.get("/help") self.assertEqual(get_help_response.request["PATH_INFO"], "/help") logout_response = self.flinstones.post("/logout") self.assertRedirects(logout_response, "/", 302) def test_user_name_disappears(self): """ Test that if a user has logged out of any page - That the page he is redirected to does not contain his first name """ after_login_response = self.flinstones.get("/") self.assertContains(after_login_response, 'flinstones') self.flinstones.post("/logout") after_logout_response = self.flinstones.get("/") self.assertNotContains(after_logout_response, 'flinstones') def test_logout_creates_session_key(self): """ Test that logging out creates a session key """ after_login_response = self.flinstones.get("/") self.assertTrue(after_login_response.context["user"].is_authenticated) self.assertTrue(self.flinstones.session.session_key) self.flinstones.post("/logout") after_logout_response = self.flinstones.get("/") self.assertFalse( after_logout_response.context["user"].is_authenticated) self.assertTrue(self.flinstones.session.session_key)
class DashboardLogic(BaseTestCase): """Test that the dashboard behaves as expected.""" def setUp(self): super(DashboardLogic, self).setUp() user_data = UserSignupTestCase().generate_user_data({}) response = self.client.post('/signup', user_data) html_content = "Please confirm your email address" self.assertContains(response, html_content) self.uriel = Client() user = User.objects.get(email="*****@*****.**") user.is_active = True user.save() self.uriel.force_login(user) def test_name_edition(self): ''' Test that when a logged in user edits his first and last name by entering the data correctly that: - The user remains on the dashboard page. - The first name is changed properly - The last name is changed properly ''' first_name_data = {"first_name": "Britney"} response = self.uriel.post('/dashboard', first_name_data) self.assertEqual(response.status_code, 200) get_response = self.uriel.get('/dashboard') self.assertContains(get_response, "Britney") last_name_data = {"last_name": "Delila"} response = self.uriel.post('/dashboard', last_name_data) self.assertEqual(response.status_code, 200) get_response = self.uriel.get('/dashboard') self.assertContains(get_response, "Delila") def test_contact_edition(self): ''' Test that when a logged in user edits his country code and phone number by entering the data correctly that: - The user remains on the dashboard page. - The country code is changed properly - The last name is changed properly ''' CountryCode.objects.create(country_code=256, country="Uganda") country_code = CountryCode.objects.filter(country_code=256).first() country_code_data = {"country_code": country_code.id} response = self.uriel.post('/dashboard', country_code_data) self.assertEqual(response.status_code, 200) get_response = self.uriel.get('/dashboard') self.assertContains(get_response, str(country_code)) phone_data = {"phone_number": 220000} response = self.uriel.post('/dashboard', phone_data) self.assertEqual(response.status_code, 200) get_response = self.uriel.get('/dashboard') self.assertContains(get_response, 220000) def test_old_password_validation(self): ''' Test that when a user enters the wrong old password: - The user remains on the same page and is not redirected. - The user is prompted with an error message Test that when a user enters the correct old password: - The user is redirected to a page to edit the password ''' response = self.uriel.post('/old_password', { "old_password": "******" }) self.assertEqual(response.status_code, 200) error_message = "Your old password was entered incorrectly. " "Please enter it again." self.assertContains(response, error_message) response_1 = self.uriel.post('/old_password', { "old_password": "******" }) self.assertRedirects(response_1, "/change_password", 302) def test_view_login_required(self): ''' Test that if a user has not been logged in that: - He cannot access the dashboard view. - He cannot access the old password view. - He cannot access the change password view. - He is redirected to the login view ''' old_password_view = self.client.get("/old_password") self.assertRedirects( old_password_view, "/login?next=/old_password", 302 ) dashboard_view = self.client.get('/dashboard') self.assertRedirects( dashboard_view, "/login?next=/dashboard", 302 ) change_password_view = self.client.get('/change_password') self.assertRedirects( change_password_view, "/login?next=/change_password", 302 ) def test_change_password_old_password_validation(self): ''' Test that when a logged in user manually goes to the change_password view that: - He cannot access the view - He is redirected to the old_password view inorder to insert his old password. ''' change_password_view = self.uriel.get('/change_password') self.assertRedirects(change_password_view, "/old_password", 302) def test_change_password(self): ''' Test that when a user is changing a password that: - He is redirected to the login page ''' self.uriel.post('/old_password', {"old_password": "******"}) response = self.uriel.post('/change_password', { "new_password1": "!@£$!@£$!@£$£!@$!@£$!@3", "new_password2": "!@£$!@£$!@£$£!@$!@£$!@3" }) self.assertRedirects(response, "/login", 302)
class SearchTest(BaseTestCase): """Test search produces the expected results.""" def setUp(self): User.objects.create_user(email="*****@*****.**", password="******") self.user = User.objects.get(email="*****@*****.**") self.sivanna = Client() super(SearchTest, self).setUp() def test_search_get_request(self): ''' Test that when you use a get method to request for the search page - That the page redirects to the landing page ''' response = self.sivanna.get("/search") self.assertContains( response, "Add something to the search bar to" " search for your items") def test_search_phone_name(self): ''' Test that when you search for a phone name: - That the phone object is rendered on the search response ''' response = self.sivanna.post("/search", {"search-name": "sam"}) self.assertContains(response, self.samsung_note_5_rose_gold.phone_model) def test_search_phone_price(self): ''' Test that whe you search for a phone price: - That the phone object is rendered on the search response ''' response = self.sivanna.post("/search", {"search-name": "25"}) self.assertContains(response, self.samsung_note_5_rose_gold.phone_model) def test_search_phone_size(self): ''' Test that when you search for a phone price: - That the phone object is rendered on the search response ''' response = self.sivanna.post("/search", {"search-name": "16"}) self.assertContains(response, self.samsung_note_5_rose_gold.phone_model) def test_search_phone_category(self): ''' Test that when you enter a category name in the search bar: - That you get phone objects in that category ''' response = self.sivanna.post("/search", {"search-name": "andro"}) self.assertContains(response, self.samsung_note_5_rose_gold.phone_model) self.assertContains(response, self.samsung_note_7_rose_gold.phone_model) self.assertNotContains(response, self.iphone_6_s_rose_gold.phone_model) def test_search_phone_feature(self): ''' Test that when you enter a phone feature in the search bar: - That you get phone objects with that feature ''' Feature.objects.create(phone=self.samsung_note_7_rose_gold, feature="6-inch screen") Feature.objects.create(phone=self.iphone_6_s_rose_gold, feature="7-inch screen") response = self.sivanna.post("/search", {"search-name": "inch"}) self.assertContains(response, self.samsung_note_7_rose_gold.phone_model) self.assertContains(response, self.iphone_6_s_rose_gold.phone_model) self.assertNotContains(response, self.samsung_note_5_rose_gold.phone_model) def test_search_product_information(self): ''' Test that when you enter a phone product information in the \ search bar: - That you get phone objects with that product information ''' ProductInformation.objects.create(phone=self.samsung_note_7_rose_gold, feature="Network", value="GSM") ProductInformation.objects.create(phone=self.iphone_6_s_rose_gold, feature="Network", value="GSM") response = self.sivanna.post("/search", {"search-name": "gsm"}) self.assertContains(response, self.samsung_note_7_rose_gold.phone_model) self.assertContains(response, self.iphone_6_s_rose_gold.phone_model) self.assertNotContains(response, self.samsung_note_5_rose_gold.phone_model) self.assertContains(response, self.samsung_note_7_rose_gold.phone_model) self.assertContains(response, self.iphone_6_s_rose_gold.phone_model) self.assertNotContains(response, self.samsung_note_5_rose_gold.phone_model) def test_search_product_review(self): ''' Test that when you enter a phone review in the search bar: - That you get phone objects with that product information ''' authentic_comment = "That was an authentic phone that I got" genuine_comment = "That was an genuine phone that I got" Review.objects.create(stars=5, comments=authentic_comment, phone_model=self.samsung_note_5, owner=self.user) Review.objects.create(stars=4, comments=genuine_comment, phone_model=self.samsung_note_7, owner=self.user) response = self.sivanna.post("/search", {"search-name": "genui"}) self.assertNotContains(response, self.samsung_note_5_rose_gold.phone_model) self.assertContains(response, self.samsung_note_7_rose_gold.phone_model) self.assertNotContains(response, self.iphone_6_s_rose_gold.phone_model) response_2 = self.sivanna.post("/search", {"search-name": "auth"}) self.assertContains(response_2, self.samsung_note_5_rose_gold.phone_model) self.assertNotContains(response_2, self.samsung_note_7_rose_gold.phone_model) self.assertNotContains(response_2, self.iphone_6_s_rose_gold.phone_model) def test_not_in_stock_search(self): ''' Test that for phones that are not in stock, or their quantity is zero: - They won't be rendered on search ''' PhoneModel.objects.create(category=self.iphone, brand=self.apple_brand, brand_model="Iphone 6 J", average_review=5.0) self.iphone_6_j = PhoneModel.objects.get(brand_model="Iphone 6 J") PhoneModel.objects.create(category=self.iphone, brand=self.apple_brand, brand_model="Iphone 7 J", average_review=5.0) self.iphone_7_j = PhoneModel.objects.get(brand_model="Iphone 7 J") PhoneModelList.objects.create(phone_model=self.iphone_6_j, currency=self.currency_v, price=25000, size_sku=self.size_iphone, color=self.color_one, quantity=0, is_in_stock=True) self.iphone_6_s_rose_gold = PhoneModelList.objects.get( phone_model=self.iphone_6_s, color=self.color_one) PhoneModelList.objects.create(phone_model=self.iphone_7_j, currency=self.currency_v, price=25000, size_sku=self.size_iphone, color=self.color_one, quantity=4, is_in_stock=False) self.iphone_7_s_rose_gold = PhoneModelList.objects.get( phone_model=self.iphone_7_j, color=self.color_one) post_response_1 = self.client.post("/search", {"search-name": "Samsung Note 5"}) post_response_3 = self.client.post("/search", {"search-name": "Iphone 6 J"}) post_response_2 = self.client.post("/search", {"search-name": "Iphone 7"}) self.assertContains(post_response_1, "Samsung Note 5") self.assertNotContains(post_response_2, "Iphone 7") self.assertNotContains(post_response_3, "Iphone 6")