示例#1
0
 def test_user_rights(self):
     restricted_urls = [
         x
         for x in self.urls_to_test
         if x in ("books:add", "books:add_tag", "books:add_author")
         or (type(x) is TupleType and x[0] in ("books:delete", "books:change"))
     ]
     allowed_urls = list(set(self.urls_to_test) - set(restricted_urls))
     user = create_random_user()
     self.client.login(username=user[0].username, password=user[1])
     delta_percent = count_delta(len(self.urls_to_test))
     percentage = delta_percent
     for url in allowed_urls:
         percentage = write_percentage(percentage, delta_percent)
         if type(url) is TupleType:
             urln = url[0]
             urlp = url[1]
             request = self.client.get(reverse(urln, kwargs={"pk": urlp}))
         else:
             urln = url
             request = self.client.get(reverse(urln))
         if request.status_code == 200:
             self.assertEqual(request.status_code, 200)
         else:
             self.assertTrue(not request["location"].startswith("http://testserver/auth/login?next="))
     for url in restricted_urls:
         percentage = write_percentage(percentage, delta_percent)
         if type(url) is TupleType:
             urln = url[0]
             urlp = url[1]
             request = self.client.get(reverse(urln, kwargs={"pk": urlp}))
         else:
             urln = url
             request = self.client.get(reverse(urln))
         self.assertEqual(request.status_code, 302)
示例#2
0
 def test_user_rights(self):  # User and admin have equal rights here
     urls_to_test = [x for x in self.urls_to_test if not (type(x) is TupleType and x[0].endswith('change'))]
     restricted_urls = [x for x in self.urls_to_test if type(x) is TupleType and x[0].endswith('change')]
     delta_percent = count_delta(len(self.users + self.admins))
     percentage = float(0)
     for user in self.users + self.admins:
         percentage = write_percentage(percentage, delta_percent)
         self.client.login(username=user[0].username, password=user[1])
         for url in urls_to_test:
             if type(url) is TupleType:
                 urln = url[0]
                 urlp = url[1]
                 request = self.client.get(reverse(urln, kwargs={'pk': urlp}))
             else:
                 urln = url
                 request = self.client.get(reverse(urln))
             if request.status_code == 200:
                 self.assertEqual(request.status_code, 200)
         for url in restricted_urls:
             if type(url) is TupleType:
                 urln = url[0]
                 urlp = url[1]
                 request = self.client.get(reverse(urln, kwargs={'pk': urlp}))
             if user[0].pk == urlp:
                 self.assertEqual(request.status_code, 200)
             else:
                 self.assertEqual(request.status_code, 302)
         self.client.logout()
示例#3
0
 def test_add_author(self):
     authors_count = 0
     delta_percent = count_delta(NUMBER_OF_ITERATIONS_AUTHORS)
     percentage = delta_percent
     for i in range(1, NUMBER_OF_ITERATIONS_AUTHORS):
         percentage = write_percentage(percentage, delta_percent)
         will_be_an_error = random.randint(0, 1)
         was_error = False
         if will_be_an_error and random.randint(0, 1):  # it will be in this field
             first_name = random_string(size=(random.randint(46, 100)))
             was_error = True
         else:
             first_name = random_string(size=(random.randint(1, 45)))
         if will_be_an_error and (random.randint(0, 1) or not was_error):  # read "and it will be in this field"
             last_name = random_string(size=(random.randint(46, 100)))
         else:
             last_name = random_string(size=(random.randint(1, 45)))
         request = self.client.post(reverse("books:add_author"), {"first_name": first_name, "last_name": last_name})
         if not will_be_an_error:
             authors_count += 1
             self.assertEqual(request.status_code, 302)
             self.assertEqual("http://testserver/", request["location"])
             new_author = Author.authors.get(pk=authors_count)
             self.assertEqual(new_author.first_name, first_name)
             self.assertEqual(new_author.last_name, last_name)
         else:
             self.assertTrue(not request.context_data["form"].is_valid())
示例#4
0
 def test_availability(self):
     delta_percent = count_delta(len(self.urls_to_test))
     percentage = float(0)
     for url in self.urls_to_test:
         percentage = write_percentage(percentage, delta_percent)
         urln = url
         request = self.client.get(reverse(urln))
         self.assertTrue(request.status_code != 404)
示例#5
0
 def test_logged_out_rights(self):
     delta_percent = count_delta(len(self.urls_to_test))
     percentage = float(0)
     for url in self.urls_to_test:
         percentage = write_percentage(percentage, delta_percent)
         if type(url) is TupleType:
             urln = url[0]
             urlp = url[1]
             request = self.client.get(reverse(urln, kwargs={'pk': urlp}))
         else:
             urln = url
             request = self.client.get(reverse(urln))
         self.assertTrue(request['location'].startswith('http://testserver/auth/login?next='))
示例#6
0
 def test_availability(self):
     delta_percent = count_delta(len(self.urls_to_test))
     percentage = float(0)
     for url in self.urls_to_test:
         percentage = write_percentage(percentage, delta_percent)
         if type(url) is TupleType:
             urln = url[0]
             urlp = url[1]
             request = self.client.get(reverse(urln, kwargs={'pk': urlp}))
         else:
             urln = url
             request = self.client.get(reverse(urln))
         self.assertTrue(request.status_code != 404)
示例#7
0
    def test_registration(self):
        users_count = 0
        delta_percent = count_delta(NUMBER_OF_ITERATIONS)
        percentage = delta_percent
        for i in range(1, NUMBER_OF_ITERATIONS):
            percentage = write_percentage(percentage, delta_percent)
            username = random_string(size=(random.randint(1, 26)), chars=string.letters) + str(i)
            password = random_string(random.randint(8, MAX_PASSWORD_LENGTH))
            email = random_string(size=random.randint(2, MAX_EMAIL_LENGTH),
                                                    chars=string.letters + string.digits,
                                                    at_least_one_char=True)+"@gmail.com"
            pass_are_equal = random.randint(0, 1)
            if pass_are_equal:  # randomize passwords equality
                password2 = password
            else:
                password2 = password
                list_pass = list(password2)
                list_pass = chr(ord(password2[0]) + 1)
                password2 = ''.join(list_pass)
            request = self.client.post(reverse('registration_register'),
                                       {'username': username,
                                        'password1': password,
                                        'password2': password2,
                                        'email': email,
                                        'recaptcha_response_field': 'PASSED'})
            if pass_are_equal:
                users_count += 1
                add = 1
                self.assertEqual(302, request.status_code)
                self.assertEqual('http://testserver/accounts/register/complete/', request['location'])
                new_user = User.objects.get(pk=users_count)
                self.assertEqual(new_user.is_active, False)
                new_profile = RegistrationProfile.objects.get(pk=users_count)
                request = self.client.get("http://testserver/accounts/activate/" + new_profile.activation_key +'/')
                new_user = User.objects.get(pk=users_count)
                self.assertEqual(new_user.is_active, True)

                request = self.client.post(reverse('auth_login'), {'username': new_user.username,
                                                                   'password': password})
                new_user = User.objects.get(pk=users_count)
                self.assertEqual('http://testserver/', request['location'])
                delta = new_user.last_login - timezone.now()
                self.assertTrue(delta.total_seconds() < 1)
            else:
                self.assertTrue(not request.context_data['form'].is_valid())
示例#8
0
 def test_search(self):
     tags_count = 0
     delta_percent = count_delta(NUMBER_OF_ITERATIONS_SEARCH)
     percentage = delta_percent
     for i in range(1, NUMBER_OF_ITERATIONS_SEARCH):
         percentage = write_percentage(percentage, delta_percent)
         book_free = Book.books.create(title="BUGABUGA")
         book_busy = Book.books.create(title="BUGABUGA")
         book_busy.busy = True
         book_busy.save()
         will_be_an_error = random.randint(0, 1)
         if will_be_an_error:
             while True:
                 tags = random_string(size=(random.randint(70, 100)))
                 if " BUGABUGA " in tags:
                     continue
                 was_error = True
                 break
         else:
             tags = random_string(size=(random.randint(0, 80 - len(" BUGABUGA "))))
             position = random.randint(0, len(tags))
             tags = tags[:position] + " BUGABUGA " + tags[position:]
         request = self.client.get(reverse("books:list"), {"keywords": tags})
         if not will_be_an_error:
             book_busy = Book.books.get(pk=2)
             book_free = Book.books.get(pk=1)
             self.assertTrue(
                 book_free in request.context_data["object_list"]
                 and book_busy in request.context_data["object_list"]
             )
             request = self.client.get(reverse("books:list"), {"keywords": tags, "busy": "1"})
             book_busy = Book.books.get(pk=2)
             book_free = Book.books.get(pk=1)
             self.assertTrue(
                 book_free in request.context_data["object_list"]
                 and book_busy not in request.context_data["object_list"]
             )
             request = self.client.get(reverse("books:list"), {"keywords": tags, "busy": "2"})
             book_busy = Book.books.get(pk=2)
             book_free = Book.books.get(pk=1)
             self.assertTrue(
                 book_free not in request.context_data["object_list"]
                 and book_busy in request.context_data["object_list"]
             )
示例#9
0
 def test_admin_rights(self):
     user = create_random_user()
     user[0].is_staff = True
     user[0].save()
     authenticate(username=user[0].username, password=user[1])
     self.client.login(username=user[0].username, password=user[1])
     delta_percent = count_delta(len(self.urls_to_test))
     percentage = delta_percent
     for url in self.urls_to_test:
         percentage = write_percentage(percentage, delta_percent)
         if type(url) is TupleType:
             urln = url[0]
             urlp = url[1]
             request = self.client.get(reverse(urln, kwargs={"pk": urlp}))
         else:
             urln = url
             request = self.client.get(reverse(urln))
         if request.status_code == 200:
             self.assertEqual(request.status_code, 200)
         else:
             self.assertTrue(not request["location"].startswith("http://testserver/auth/login?next="))
示例#10
0
 def test_add_tag(self):
     tags_count = 0
     delta_percent = count_delta(NUMBER_OF_ITERATIONS_TAGS)
     percentage = delta_percent
     for i in range(1, NUMBER_OF_ITERATIONS_TAGS):
         percentage = write_percentage(percentage, delta_percent)
         will_be_an_error = random.randint(0, 1)
         was_error = False
         if will_be_an_error:  # it will be in this field
             tag = random_string(size=(random.randint(21, 100)))
             was_error = True
         else:
             tag = random_string(size=(random.randint(1, 20)))
         request = self.client.post(reverse("books:add_tag"), {"tag": tag})
         if not will_be_an_error:
             tags_count += 1
             self.assertEqual(request.status_code, 302)
             self.assertEqual("http://testserver/", request["location"])
             new_tag = Book_Tag.tags.get(pk=tags_count)
             self.assertEqual(new_tag.tag, tag)
         else:
             self.assertTrue(not request.context_data["form"].is_valid())
示例#11
0
    def test_take_return_ask_successfully(self):
        user1 = create_random_user()
        user2 = create_random_user()
        delta_percent = count_delta(NUMBER_OF_ITERATIONS_A_T_R)
        percentage = delta_percent
        for i in range(1, NUMBER_OF_ITERATIONS_A_T_R):
            percentage = write_percentage(percentage, delta_percent)
            book = Book.books.create(title="test_book" + str(i))
            # first_user asks free book
            self.client.login(username=user1[0].username, password=user1[1])
            request = self.client.get(reverse("books:ask", kwargs={"pk": book.pk}))
            self.assertEqual(request.status_code, 302)
            self.assertTrue(request["location"] == "http://testserver/books/")  # You can't ask free book
            # and take it
            request = self.client.get(reverse("books:take", kwargs={"pk": book.pk}))
            self.assertTrue(request["location"] == "http://testserver/books/")  # You can take free book
            # book really taken by him
            book = Book.books.get(pk=i)
            self.assertTrue(book.busy and book in user1[0].get_users_books())
            # second user logs in
            self.client.logout()
            self.client.login(username=user2[0].username, password=user2[1])
            # and tried to take busy book
            request = self.client.get(reverse("books:take", kwargs={"pk": book.pk}))
            self.assertEqual(request.status_code, 302)
            self.assertEqual(request["location"], "http://testserver/books/")  # You can't take busy book
            book = Book.books.get(pk=i)
            self.assertTrue(book not in user2[0].get_users_books())
            # and to return
            request = self.client.get(reverse("books:return", kwargs={"pk": book.pk}))
            self.assertEqual(request.status_code, 302)
            self.assertEqual(request["location"], "http://testserver/books/")  # You can't return busy (not yours) book
            book = Book.books.get(pk=i)
            self.assertTrue(book.busy and book in user1[0].get_users_books())
            # he asks this book
            request = self.client.get(reverse("books:ask", kwargs={"pk": book.pk}))
            self.assertEqual(request.status_code, 200)  # You can ask busy book
            # and secondone logs in
            self.client.logout()
            self.client.login(username=user1[0].username, password=user1[1])
            # he returns book and book is returned
            request = self.client.get(reverse("books:return", kwargs={"pk": book.pk}))
            self.assertEqual(request["location"], "http://testserver/books/")  # You can return book
            book = Book.books.get(pk=i)
            self.assertTrue(not book.busy and book not in user1[0].get_users_books())
            # first user logs in
            self.client.logout()
            self.client.login(username=user2[0].username, password=user2[1])
            # takes book
            request = self.client.get(reverse("books:take", kwargs={"pk": book.pk}))
            self.assertEqual(request["location"], "http://testserver/books/")
            book = Book.books.get(pk=i)
            self.assertTrue(
                book.busy and (book in user2[0].get_users_books())
            )  # You can take free book after it was taken
            # looks at it in his profile
            request = self.client.get(reverse("profile:profile", kwargs={"pk": user2[0].pk}))
            self.assertEqual(request.status_code, 200)
            # and returns it
            request = self.client.get(reverse("books:return", kwargs={"pk": book.pk}))
            self.assertEqual(request["location"], "http://testserver/books/")
            book = Book.books.get(pk=i)
            books = user2[0].get_users_books()
            self.assertTrue(not book.busy and (book not in user2[0].get_users_books()))

            self.client.logout()
示例#12
0
 def test_add_book(self):
     books_count = 0
     delta_percent = count_delta(NUMBER_OF_ITERATIONS_BOOKS)
     percentage = delta_percent
     for i in range(1, NUMBER_OF_ITERATIONS_BOOKS):
         percentage = write_percentage(percentage, delta_percent)
         there_is_isbn = random.randint(0, 1)
         will_be_an_error = random.randint(0, 1)
         was_error = False
         isbn = None
         if there_is_isbn:
             if will_be_an_error and random.randint(0, 1):  # it will be in this field
                 while True:
                     isbn = random_string(random.randint(1, 50), chars=string.digits)
                     if len(isbn) == 13 and isbn.isdigit():
                         continue
                     was_error = True
                     break
             else:
                 isbn = random_string(13, chars=string.digits)
         if will_be_an_error and random.randint(0, 1):  # it will be in this field
             title = random_string(size=(random.randint(46, 100)))
             was_error = True
         else:
             title = random_string(size=(random.randint(1, 45)))
         e_version_exists = 0
         paperback_version_exists = random.randint(0, 1)
         # if e_version_exists ==0:
         #     file = None
         # else:
         #     file = 'file'
         description = random_string(size=random.randint(1, MAX_LENGTH_OF_DESCRIPTION), chars=string.printable)
         if will_be_an_error and (random.randint(0, 1) or not was_error):
             authors = random_string(size=random.randint(1, 90), chars=string.letters)
         else:
             authors = get_success_authors_string()
         # if will_be_an_error and (random.randint(0, 1) or not was_error):
         #     tags = random_string(size=random.randint(1, 90), chars=string.letters)
         # else:
         #     tags = random_string(size=random.randint(1, MAX_LENGTH_OF_TAGS), chars=string.printable)
         form_context = {
             "title": title,
             "e_version_exists": e_version_exists,
             "paperback_version_exists": paperback_version_exists,
             "description": description,
             "file": file,
             "authors_names": authors,
         }
         if there_is_isbn:
             form_context["isbn"] = isbn
         request = self.client.post(reverse("books:add"), form_context)
         if not will_be_an_error:
             books_count += 1
             self.assertEqual(request.status_code, 302)
             self.assertEqual("http://testserver/", request["location"])
             new_book = Book.books.get(pk=books_count)
             self.assertEqual(new_book.title, title)
             self.assertEqual(new_book.description, description)
             if there_is_isbn:
                 self.assertEqual(new_book.isbn, isbn)
         else:
             self.assertTrue(not request.context_data["form"].is_valid())