class FaqsViewTest(TestCase): def setUp(self): # Every test needs a client. self.client = Client() self.user = User(id=100, username="******", name="test_user", password="******", email="*****@*****.**", role="editor") self.user.save() self.cart = Cart(user_id=self.user, guest_id=None) self.cart.save() def test_details(self): # Issue a GET request. factory = RequestFactory() request = factory.get('/faqs/') request.user = self.user response = FaqsView.as_view()(request) # Check that the response is 200 OK. self.assertEqual(response.status_code, 200) category_names = response.context_data.get('category_names') list_query = response.context_data.get('list_query') # Check that the rendered context contains 5 customers. self.assertEqual(list(category_names.values()), [a[1] for a in FAQ.FAQ_CHOICES]) self.assertTrue(len(list_query) > 0) self.user.delete() self.cart.delete()
def test_addfaq(): # Admin user user = User(id=100, username="******", name="test_user", password="******", email="*****@*****.**", role="Admin") user.save() cart = Cart(user_id=user, guest_id=None) cart.save() # Assert True body = {'category': FAQ.FAQ_CHOICES[0][1], 'question': "Test Question", 'answer': "This is my test answer"} print(body) req = RequestFactory().post("/addfaq/", body) req.user = user response = addfaq(request=req) assert response.status_code == 200 # Bad category body = {'category': "WRONG_CATEGORY", 'question': "Test Question", 'answer': "This is my test answer"} print(body) req = RequestFactory().post("/addfaq/", body) req.user = user response = addfaq(request=req) assert response.status_code == 403 user.delete() cart.delete() # Not allowed user # Non Admin user user = User(id=100, username="******", name="test_notadmin", password="******", email="*****@*****.**", role="User") user.save() cart = Cart(user_id=user, guest_id=None) cart.save() body = {'category':FAQ.FAQ_CHOICES[0][1], 'question': "Test Question", 'answer': "This is my test answer"} print(body) req = RequestFactory().post("/addfaq/", body) req.user = user response = addfaq(request=req) assert response.status_code == 403 user.delete() cart.delete() # Anonymous User: Not allowed user = "******" body = {'category': FAQ.FAQ_CHOICES[0][1], 'question': "Test Question", 'answer': "This is my test answer"} print(body) req = RequestFactory().post("/addfaq/", body) req.user = user response = addfaq(request=req) assert response.status_code == 403
class UserLibraryTest(TestCase): def setUp(self): # Every test needs a client. self.client = Client() self.user = User(id=100, username="******", name="test_user", password="******", email="*****@*****.**", role="editor") self.user.save() self.cart = Cart(user_id=self.user, guest_id=None) self.cart.save() def test_details(self): # Issue a GET request. factory = RequestFactory() request = factory.get('/library/') request.user = self.user response = UserLibrary.as_view()(request) # Check that the response is 200 OK. self.assertEqual(response.status_code, 200) try: coincident = response.context_data.get('coincident') except: assert False self.user.delete() self.cart.delete()
def test_deletefaq(): # Admin user user = User(id=100, username="******", name="test_user", password="******", email="*****@*****.**", role="Admin") user.save() cart = Cart(user_id=user, guest_id=None) cart.save() original_faq = FAQ(question="Original question", category=FAQ.FAQ_CHOICES[0][0], answer="Original answer") original_faq.save() body = {'original': original_faq.question} req = RequestFactory().post("/deletefaq/", body) req.user = user response = deletefaq(request=req) assert response.status_code == 200 original_faq_query = FAQ.objects.filter(question=original_faq.question) assert (len(original_faq_query) == 0) # Faq does not exist # After de delete should not exist body = {'original': original_faq.question} req = RequestFactory().post("/deletefaq/", body) req.user = user response = deletefaq(request=req) assert response.status_code == 404 user.delete() cart.delete() # Anonymous User Forbidden user = "******" original_faq = FAQ(question="Original question", category=FAQ.FAQ_CHOICES[0][0], answer="Original answer") original_faq.save() body = {'original': original_faq.question} req = RequestFactory().post("/deletefaq/", body) req.user = user response = deletefaq(request=req) assert response.status_code == 403
def test_modifyfaq(): # Admin user user = User(id=100, username="******", name="test_user", password="******", email="*****@*****.**", role="Admin") user.save() cart = Cart(user_id=user, guest_id=None) cart.save() original_faq = FAQ(question="Original question", category=FAQ.FAQ_CHOICES[0][0], answer="Original answer") original_faq.save() body = { 'original': original_faq.question, 'category': FAQ.FAQ_CHOICES[1][1], 'question': "Test Question", 'answer': "This is my test answer" } req = RequestFactory().post("/modifyfaq/", body) req.user = user response = modifyfaq(request=req) assert response.status_code == 200 original_faq = FAQ.objects.filter(question=original_faq.question) assert (len(original_faq) == 0) modified_faq = FAQ.objects.filter(question="Test Question") assert (len(modified_faq) > 0) # Bad original body = { 'original': "This question does not exist", 'category': FAQ.FAQ_CHOICES[1][1], 'question': "Test Question", 'answer': "This is my test answer" } print(body) req = RequestFactory().post("/modifyfaq/", body) req.user = user response = modifyfaq(request=req) assert response.status_code == 403 # Bad category body = { 'original': modified_faq.first().question, 'category': "Bad category", 'question': "Test Question", 'answer': "This is my test answer" } print(body) req = RequestFactory().post("/modifyfaq/", body) req.user = user response = modifyfaq(request=req) assert response.status_code == 403 user.delete() cart.delete() # Not allowed user # Non Admin user user = User(id=100, username="******", name="test_notadmin", password="******", email="*****@*****.**", role="User") user.save() cart = Cart(user_id=user, guest_id=None) cart.save() body = { 'original': modified_faq.first().question, 'category': FAQ.FAQ_CHOICES[0][1], 'question': "Test Question", 'answer': "This is my test answer" } print(body) req = RequestFactory().post("/modifyfaq/", body) req.user = user response = modifyfaq(request=req) assert response.status_code == 403 user.delete() cart.delete() # Anonymous User: Not allowed user = "******" body = { 'original': modified_faq.first().question, 'category': FAQ.FAQ_CHOICES[0][1], 'question': "Test Question", 'answer': "This is my test answer" } print(body) req = RequestFactory().post("/modifyfaq/", body) req.user = user response = modifyfaq(request=req) assert response.status_code == 403 modified_faq.delete()