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 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()
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 create_user(random_user=False): """ Test if creation of Users has any error, creating or storing the information""" # Data to test if random_user: _id = random.randint(0, 654891898) else: _id = 1 role = 'Admin' name = 'Josep' if random_user: username = str(random.randint(0, 5156123423456015412))[:12] else: username = '******' password = '******' email = '*****@*****.**' user_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') fact_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') user_address.save() fact_address.save() # Model creation obj = User(id=_id, role=role, username=username, name=name, password=password, email=email, user_address=user_address, fact_address=fact_address) obj.save() cart = Cart(user_id=obj) cart.save() return obj
def create_user(): """ Test if creation of Users has any error, creating or storing the information""" # Data to test _id = 30 role = 'Admin' name = 'Josep' username = '******' password = '******' email = '*****@*****.**' user_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') fact_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') user_address.save() fact_address.save() # Model creation obj = User(id=_id, role=role, username=username, name=name, password=password, email=email, user_address=user_address, fact_address=fact_address) obj.save() return obj
def create_user(random_user=False): """ Test if creation of Users has any error, creating or storing the information""" # Data to test if random_user: _id = random.randint(0, 654891898) else: _id = 1 role = 'Admin' name = 'Josep' if random_user: username = str(random.randint(0, 5156123423456015412))[:12] else: username = '******' password = '******' email = '*****@*****.**' user_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') fact_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') user_address.save() fact_address.save() # Model creation obj = User(id=_id, role=role, username=username, name=name, password=password, email=email, user_address=user_address, fact_address=fact_address) obj.save() # group = Group.objects.create(name=str(random.randint(0, 5156123423456015412))[:6]) # content_type = ContentType.objects.get_for_model(Book) # permission = Permission.objects.get( # codename='add_book', # content_type=content_type, # ) # perm = Permission.objects.get(codename='add_book') # print(perm) # group.permissions.add(perm) # obj.groups.add(group) #obj.user_permissions.add(permission) # obj.save() perms = Permission.objects.filter(codename__in=('add_book', )) obj.user_permissions.add(*perms) return obj
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_user(): """ Test if creation of Users has any error, creating or storing the information""" # Data to test _id = 30 role = 'Admin' name = 'Josep' username = '******' password = '******' email = '*****@*****.**' user_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') fact_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') user_address.save() fact_address.save() # Model creation obj = User(id=_id, role=role, username=username, name=name, password=password, email=email, user_address=user_address, fact_address=fact_address) obj.save() # Retrieve model to check correct creation obj = User.objects.filter(id=_id).last() print([ _id == obj.id, role == obj.role, name == obj.name, password == obj.password, email == obj.email, user_address == obj.user_address, fact_address == obj.fact_address ]) print('\n\n\n\n\n\n\n\n\n\n\n\n') print(_id, role, name, password, email, user_address, fact_address) print(obj.id, obj.role, obj.name, obj.password, obj.email, obj.user_address, obj.fact_address) print('\n\n\n\n\n\n\n\n\n\n\n\n') check = all([ _id == obj.id, role == obj.role, name == obj.name, password == obj.password, email == obj.email, user_address == obj.user_address, fact_address == obj.fact_address ]) # Test sucess if check is True assert True
def reg(request): if request.method == 'POST': data = request.POST lg_data=Udata(data)#login data instantiation user_check=User.objects.filter(uname=lg_data.uname) if user_check.exists(): u_check = "用户名已存在" else: if lg_data.passwd != lg_data.passwd2: u_check = "两次验证码不一致" else: db=User(uname=lg_data.uname,passwd=lg_data.passwd,sex=lg_data.sex) db.save() u_check = "注册成功,请登录" return render_to_response("books/reg.html",locals(),context_instance=RequestContext(request))
def get_or_create_user(): user_query = User.objects.filter(id=1000) if user_query.count() == 0: user_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') fact_address = Address(city='Barcelona', street='C/ Test, 112', country='Spain', zip='08942') user = User(id=1000, role='Admin', username='******', name='Test', password='******', email='*****@*****.**', user_address=user_address, genre_preference_1='CRIM', genre_preference_2='FANT', genre_preference_3='KIDS', fact_address=fact_address) user_address.save() fact_address.save() user.save() else: user = user_query.first() return user
def registe_do(request): data = {} name = request.REQUEST.get('username', '') password = request.REQUEST.get('password', '') try: User.objects.get(username=name) data['result'] = False data['message'] = 'The username has already existed' except: if len(name) != 0 and len(password) != 0: p1 = User(username=name, password=password) p1.save() data['result'] = True data['message'] = 'create user success' else: data['result'] = False data['message'] = "The username or password can not be empty" return JsonResponse(data)
def reg(request): if request.method == 'POST': data = request.POST lg_data = Udata(data) #login data instantiation user_check = User.objects.filter(uname=lg_data.uname) if user_check.exists(): u_check = "用户名已存在" else: if lg_data.passwd != lg_data.passwd2: u_check = "两次验证码不一致" else: db = User(uname=lg_data.uname, passwd=lg_data.passwd, sex=lg_data.sex) db.save() u_check = "注册成功,请登录" return render_to_response("books/reg.html", locals(), context_instance=RequestContext(request))
print("ADDRESS SAVED...OK") # Model creation user = User(id=15, role='Admin', username=str(random.randint(0, 5156123423456015412)), name='Josep', password='******', email='*****@*****.**', user_address=user_address, genre_preference_1='CRIM', genre_preference_2='FANT', genre_preference_3='KIDS', fact_address=fact_address) user.save() print("USER SAVED...OK") book = Book(ISBN="01234565", user_id=user, title="Harry", saga="Potter", description="first harry book", author="J.K.Rowling", price=30, language="Spanish", publisher="Alejandria", num_pages=200, num_sold=100, primary_genre="FANT",
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()