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 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_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 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 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 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))
def login (request): requestcontext = RequestContext( request) username = request.POST['username'] password = request.POST['password'] u = User.authenticate(username, password); if not u: request.session['error'] ="Wrong username/password combination" return handle_403(request) request.session['user']= u return blist(request)
def test_delete_product_guest(): guest = get_or_create_guest() cart = get_or_create_guest_cart(guest) user = User() books = get_or_create_books(user, 5) book = books[0] body = {} req = RequestFactory().post("/cart/", body) req.COOKIES['device'] = guest.device req.user = None delete_product(req, book) assert cart.books.filter(ISBN=book.ISBN).last() is None
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 register(): if current_user.is_authenticated: return redirect(url_for('search')) form = RegistrationForm() if form.validate_on_submit(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash('Your account has been created! You are now able to log in', 'success') return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
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()
def create_users(start_value, end_value): user_list = [] for value in range(start_value, end_value): user_list.append(User(user_name='u' + str(value))) User.objects.bulk_create(user_list) return 'Done'
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
import django import requests os.environ.setdefault("DJANGO_SETTINGS_MODULE", "Alejandria.settings") django.setup() from django.db.utils import IntegrityError # pylint: disable=import-error from books.models import Address, User, Book, GENRE_CHOICES try: adress = Address(street="anonimous", city="Terraplana", country="Illuminados", zip=12345) adress.save() user = User(role="Admin", name="admin", password="******", email="*****@*****.**", user_address=adress, fact_address=adress) user.save() except IntegrityError: user = User.objects.filter(name="admin").first() def _isbn10_to_isbn13(isbn_10): str_isbn_12 = "978" + str(isbn_10[0][:-1]) list_isbn_12 = [int(digit) for digit in str_isbn_12] _sum = 0 for i, str_num in zip(range(0, 12), list_isbn_12): number = int(str_num) prod = number * 1 if (i % 2 == 1) else number * 3 _sum += prod residual = _sum % 10 last_digit = 0 if residual == 0 else 10 - residual
thumb9 = File( open('Alejandria/static/images/cover-images/Night_pb-eb-des2.jpg', 'rb')) thumb10 = File( open( 'Alejandria/static/images/cover-images/design-for-writers-book-cover-tf-2-a-million-to-one.jpg', 'rb')) 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",