示例#1
0
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()
示例#3
0
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
示例#4
0
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
示例#5
0
def get_or_create_user_cart(user):
    cart_query = Cart.objects.filter(user_id=user)
    if cart_query.count() == 0:
        cart = Cart(user_id=user)
        cart.save()
    else:
        cart = cart_query.first()
    return cart
示例#6
0
def get_or_create_guest_cart(guest):
    cart_query = Cart.objects.filter(guest_id=guest)
    if cart_query.count() == 0:
        cart = Cart(guest_id=guest)
        cart.save()
    else:
        cart = cart_query.first()
    return cart
示例#7
0
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
示例#8
0
 def after_signup(self, request, user, emailAddress, **kwargs):
     profile = None
     
     try:
         profile = Profile.objects.get(user=user)
     except Profile.DoesNotExist:
         profile = Profile(user=user)
     
     profile.name = self.cleaned_data.get("nickname")
     #profile.city = City.objects.getById(id=2)  #TODO, load from cities cache
     profile.save()
     
     #=======================================================
     #code in the bookstore
     '''为注册用户创建购物车'''
     try:
         cart = Cart.objects.get(owner=profile)
     except Cart.DoesNotExist:
         cart = Cart(owner=profile)
     
     cart.save()
示例#9
0
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()
示例#10
0
 def post_save(self, obj, created=False):
     if created:
         cart = Cart(user=obj)
         cart.save()