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()