def register(request):
    if request.method == 'POST':
        formUser = f.FormReg(request.POST)
        if formUser.is_valid():
            formUser.save()
            NewCustomUser = CustomUser(user=formUser.instance)
            NewCustomUser.save()
            user = authenticate(username=request.POST['username'],
                                password=request.POST['password1'])
            login(request, user)
        return render(request,
                      template_name='reg.html',
                      context={'form': formUser})
    elif 'code' in request.GET:
        auth_code = request.GET.get('code')
        auth_token = yandex_oauth.get_oauth_json(auth_code)['access_token']
        user_json = yandex_oauth.get_account_info(auth_token)
        return render(request,
                      template_name='reg.html',
                      context={
                          'login': user_json['login'],
                          'email': user_json['default_email']
                      })
    return render(request,
                  template_name='reg.html',
                  context={'registered': False})
    def setUp(self):
        self.client = APIClient()
        self.token = None
        mods.mock_query(self.client)

        user_noadmin = CustomUser(username='******')
        user_noadmin.set_password('qwerty')
        user_noadmin.save()

        user_admin = CustomUser(username='******', is_staff=True)
        user_admin.set_password('qwerty')
        user_admin.save()
Exemple #3
0
def signUp(request):
    if request.method == 'POST':  #If 'submit' button has been pressed
        formUser = FormReg(request.POST)
        if formUser.is_valid():  #If input data is correct
            #Instance of PvPGN Profile
            newPvPGNProfile = createPvPGNProfile(request.POST['username'],
                                                 request.POST['password1'],
                                                 request.POST['email'])
            #Instance of Django user
            formUser.save()
            #Instance of model which is contains relation between PvPGN profile and Django user
            NewCustomUser = CustomUser(user=formUser.instance,
                                       pvpgn_user=newPvPGNProfile)
            NewCustomUser.save()
            #And authentication
            user = authenticate(username=request.POST['username'],
                                password=request.POST['password1'])
            login(request, user)
            return redirect('/')
        return render(request,
                      template_name='signUp.html',
                      context={'form': formUser})

    return render(request, template_name='signUp.html')
Exemple #4
0
class TestOrderModel(TestCase):
    """Class for CustomUser Model test"""
    def setUp(self):
        """ Create a user object to be used by the tests """
        time_mock = datetime.datetime(2017, 4, 10, 12, 00, tzinfo=pytz.utc)
        with mock.patch('django.utils.timezone.now') as mock_time:
            mock_time.return_value = time_mock
            self.user = CustomUser(id=111,
                                   email='*****@*****.**',
                                   password='******',
                                   first_name='fname',
                                   middle_name='mname',
                                   last_name='lname')
            self.user.save()
            self.user_free = CustomUser(id=222,
                                        email='*****@*****.**',
                                        password='******',
                                        first_name='2fname',
                                        middle_name='2mname',
                                        last_name='2lname')
            self.user_free.save()

            self.author1 = Author(id=101,
                                  name="author1",
                                  surname="s1",
                                  patronymic="p1")
            self.author1.save()

            self.author2 = Author(id=102,
                                  name="author2",
                                  surname="s2",
                                  patronymic="p2")
            self.author2.save()

            self.book1 = Book(id=101,
                              name="book1",
                              description="description1",
                              count=1)
            self.book1.save()
            self.book1.authors.add(self.author1)
            self.book1.save()

            self.book2 = Book(id=102, name="book2", description="description2")
            self.book2.save()
            self.book2.authors.add(self.author2)
            self.book2.save()

            self.book3 = Book(id=103, name="book3", description="description3")
            self.book3.save()
            self.book3.authors.add(self.author1)
            self.book3.authors.add(self.author2)
            self.book3.save()

            self.order1 = Order(id=101,
                                user=self.user,
                                book=self.book1,
                                plated_end_at=TEST_DATE)
            self.order1.save()
            self.order2 = Order(id=102,
                                user=self.user,
                                book=self.book2,
                                plated_end_at=TEST_DATE)
            self.order2.save()
            self.order3 = Order(id=103,
                                user=self.user,
                                book=self.book3,
                                end_at=TEST_DATE_END,
                                plated_end_at=TEST_DATE)
            self.order3.save()

    def test__str__end_at_is_None(self):
        """Test of the CustomUser.__str__() method"""
        order_returned = str(Order.objects.get(id=101))
        order_to_expect = "'id': 101, " \
                          "'user': CustomUser(id=111), " \
                          "'book': Book(id=101), " \
                          "'created_at': '2017-04-10 12:00:00+00:00', " \
                          "'end_at': None, " \
                          "'plated_end_at': '2017-04-10 12:00:00+00:00'"

        self.assertEqual(order_returned, order_to_expect)

    def test__str__end_at_is_not_None(self):
        """Test of the CustomUser.__str__() method"""
        order = str(Order.objects.get(id=103))
        order_to_expect = "'id': 103, " \
                          "'user': CustomUser(id=111), " \
                          "'book': Book(id=103), " \
                          "'created_at': '2017-04-10 12:00:00+00:00', " \
                          "'end_at': '2017-04-25 12:00:00+00:00', " \
                          "'plated_end_at': '2017-04-10 12:00:00+00:00'"

        self.assertEqual(order, order_to_expect)

    def test__repr__(self):
        """Test of the CustomUser.__repr__() method"""
        user_returned = repr(Order.objects.get(id=102))
        user_to_expect = "Order(id=102)"

        self.assertEqual(user_returned, user_to_expect)

    def test_get_by_id_positive(self):
        """Positive test of the CustomUser.get_by_id() method"""
        order = Order.get_by_id(101)
        self.assertEqual(order.id, 101)
        self.assertEqual(order.user, self.user)
        self.assertEqual(order.book, self.book1)
        self.assertEqual(order.created_at, TEST_DATE)
        self.assertEqual(order.end_at, None)
        self.assertEqual(order.plated_end_at, TEST_DATE)

    def test_get_by_id_negative(self):
        """Negative test of the CustomUser.get_by_id() method"""
        order = Order.get_by_id(55)
        self.assertIsNone(order)

    def test_delete_by_id_positive(self):
        """ Test of the CustomUser.delete_by_id() method """
        self.assertTrue(Order.delete_by_id(103))
        self.assertRaises(Order.DoesNotExist, Order.objects.get, pk=103)
        self.assertEqual(self.book3, Book.objects.get(id=103))
        self.assertEqual(self.user, CustomUser.objects.get(id=111))

    def test_delete_by_id_negative(self):
        """ Test of the CustomUser.delete_by_id() method """
        self.assertFalse(Order.delete_by_id(999))

    def test_create_positive(self):
        """ Positive Test of the CustomUser.create method """
        time_mock = datetime.datetime(2017, 4, 10, 12, 00, tzinfo=pytz.utc)
        with mock.patch('django.utils.timezone.now') as mock_time:
            mock_time.return_value = time_mock
            order = Order.create(self.user_free, self.book2, TEST_DATE_END)
            self.assertIsInstance(order, Order)
            self.assertEqual(order.user, self.user_free)
            self.assertEqual(order.book, self.book2)
            self.assertEqual(order.created_at, TEST_DATE)
            self.assertIsNone(order.end_at)
            self.assertEqual(order.plated_end_at, TEST_DATE_END)

    def test_create_negative_not_saved_user(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""
        user = CustomUser()
        order = Order.create(user, self.book2, TEST_DATE_END)
        self.assertIsNone(order)

    def test_create_negative_limit_book(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""

        order = Order.create(self.user_free, self.book1, TEST_DATE_END)
        self.assertIsNone(order)

    def test_get_all(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""
        orders = Order.get_all()
        self.assertListEqual(orders, [self.order1, self.order2, self.order3])

    def test_get_not_returned_books(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""
        orders = Order.get_not_returned_books()
        self.assertListEqual(orders, [self.order1, self.order2])

    def test_update_plated_end_at(self):
        order = Order.objects.get(id=101)
        new_date = TEST_DATE_END + datetime.timedelta(days=4)
        order.update(plated_end_at=new_date)

        order = Order.objects.get(id=101)
        self.assertEqual(order.id, 101)
        self.assertEqual(order.user, self.user)
        self.assertEqual(order.book, self.book1)
        self.assertEqual(order.created_at, TEST_DATE)
        self.assertEqual(order.end_at, None)
        self.assertEqual(order.plated_end_at, new_date)

    def test_update_end_at(self):
        order = Order.objects.get(id=101)
        new_date = TEST_DATE_END + datetime.timedelta(days=4)
        order.update(end_at=new_date)

        order = Order.objects.get(id=101)
        self.assertEqual(order.id, 101)
        self.assertEqual(order.user, self.user)
        self.assertEqual(order.book, self.book1)
        self.assertEqual(order.created_at, TEST_DATE)
        self.assertEqual(order.end_at, new_date)
        self.assertEqual(order.plated_end_at, TEST_DATE)
Exemple #5
0
class TestAuthorModel(TestCase):
    """Class for CustomUser Model test"""
    def setUp(self):
        """ Create a user object to be used by the tests """
        time_mock = datetime.datetime(2017, 4, 10, 12, 00, tzinfo=pytz.utc)
        with mock.patch('django.utils.timezone.now') as mock_time:
            mock_time.return_value = time_mock
            self.user = CustomUser(id=111,
                                   email='*****@*****.**',
                                   password='******',
                                   first_name='fname',
                                   middle_name='mname',
                                   last_name='lname')
            self.user.save()
            self.user_free = CustomUser(id=222,
                                        email='*****@*****.**',
                                        password='******',
                                        first_name='2fname',
                                        middle_name='2mname',
                                        last_name='2lname')
            self.user_free.save()

            self.author1 = Author(id=101,
                                  name="author1",
                                  surname="s1",
                                  patronymic="p1")
            self.author1.save()

            self.author2 = Author(id=102,
                                  name="author2",
                                  surname="s2",
                                  patronymic="p2")
            self.author2.save()

            self.book1 = Book(id=101,
                              name="book1",
                              description="description1",
                              count=1)
            self.book1.save()
            self.book1.authors.add(self.author1)
            self.book1.save()

            self.book2 = Book(id=102, name="book2", description="description2")
            self.book2.save()
            self.book2.authors.add(self.author2)
            self.book2.save()

            self.book3 = Book(id=103, name="book3", description="description3")
            self.book3.save()
            self.book3.authors.add(self.author1)
            self.book3.authors.add(self.author2)
            self.book3.save()

            self.order1 = Order(id=101,
                                user=self.user,
                                book=self.book1,
                                plated_end_at=TEST_DATE)
            self.order1.save()
            self.order2 = Order(id=102,
                                user=self.user,
                                book=self.book2,
                                plated_end_at=TEST_DATE)
            self.order2.save()
            self.order3 = Order(id=103,
                                user=self.user,
                                book=self.book3,
                                end_at=TEST_DATE_END,
                                plated_end_at=TEST_DATE)
            self.order3.save()

    def test__str__(self):
        """Test of the CustomUser.__str__() method"""
        author_returned = str(Author.objects.get(id=101))
        author_to_expect = "'id': 101, 'name': 'author1', 'surname': 's1', 'patronymic': 'p1'"

        self.assertEqual(author_returned, author_to_expect)

    def test__repr__(self):
        """Test of the CustomUser.__repr__() method"""
        author_returned = repr(Author.objects.get(id=102))
        author_to_expect = "Author(id=102)"

        self.assertEqual(author_returned, author_to_expect)

    def test_get_by_id_positive(self):
        """Positive test of the CustomUser.get_by_id() method"""
        author = Author.get_by_id(101)
        self.assertEqual(author.id, 101)
        self.assertEqual(author.name, 'author1')
        self.assertEqual(author.surname, "s1")
        self.assertEqual(author.patronymic, "p1")

    def test_get_by_id_negative(self):
        """Negative test of the CustomUser.get_by_id() method"""
        author = Author.get_by_id(999)
        self.assertIsNone(author)

    def test_delete_by_id_positive(self):
        """ Test of the CustomUser.delete_by_id() method """
        self.assertTrue(Author.delete_by_id(101))
        self.assertRaises(Author.DoesNotExist, Author.objects.get, pk=101)

    def test_delete_by_id_negative(self):
        """ Test of the CustomUser.delete_by_id() method """
        self.assertFalse(Author.delete_by_id(999))

    def test_get_all(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""
        authors = list(Author.get_all())
        authors.sort(key=lambda author: author.id)
        self.assertListEqual(authors, [self.author1, self.author2])

    def test_update(self):
        author = Author.objects.get(id=101)
        author.update(name="testName",
                      surname="testSurname",
                      patronymic="testPatronymic")

        author = Author.objects.get(id=101)
        self.assertIsInstance(author, Author)
        self.assertEqual(author.name, "testName")
        self.assertEqual(author.surname, "testSurname")
        self.assertEqual(author.patronymic, "testPatronymic")

    def test_update_only_name(self):
        author = Author.objects.get(id=101)
        author.update(name="testName")

        author = Author.objects.get(id=101)
        self.assertIsInstance(author, Author)
        self.assertEqual(author.name, "testName")
        self.assertEqual(author.surname, "s1")
        self.assertEqual(author.patronymic, "p1")

    def test_update_not_valid_name(self):
        author = Author.objects.get(id=101)
        author.update(name="testName" * 5)

        author = Author.objects.get(id=101)
        self.assertIsInstance(author, Author)
        self.assertEqual(author.name, "author1")
        self.assertEqual(author.surname, "s1")
        self.assertEqual(author.patronymic, "p1")

    def test_create(self):
        author = Author.create(name="testName", surname="s1", patronymic="p1")
        author = Author.objects.get(id=author.id)
        self.assertIsInstance(author, Author)
        self.assertEqual(author.name, "testName")
        self.assertEqual(author.surname, "s1")
        self.assertEqual(author.patronymic, "p1")

    def test_createnot_valid_surname(self):
        author = Author.create(name="testName",
                               surname="s1" * 20,
                               patronymic="p1")
        self.assertIsNone(author)
Exemple #6
0
class TestBookModel(TestCase):
    """Class for CustomUser Model test"""
    def setUp(self):
        """ Create a user object to be used by the tests """
        time_mock = datetime.datetime(2017, 4, 10, 12, 00, tzinfo=pytz.utc)
        with mock.patch('django.utils.timezone.now') as mock_time:
            mock_time.return_value = time_mock
            self.user = CustomUser(id=111,
                                   email='*****@*****.**',
                                   password='******',
                                   first_name='fname',
                                   middle_name='mname',
                                   last_name='lname')
            self.user.save()
            self.user_free = CustomUser(id=222,
                                        email='*****@*****.**',
                                        password='******',
                                        first_name='2fname',
                                        middle_name='2mname',
                                        last_name='2lname')
            self.user_free.save()

            self.author1 = Author(id=101,
                                  name="author1",
                                  surname="s1",
                                  patronymic="p1")
            self.author1.save()

            self.author2 = Author(id=102,
                                  name="author2",
                                  surname="s2",
                                  patronymic="p2")
            self.author2.save()

            self.book1 = Book(id=101,
                              name="book1",
                              description="description1",
                              count=1)
            self.book1.save()
            self.book1.authors.add(self.author1)
            self.book1.save()

            self.book2 = Book(id=102, name="book2", description="description2")
            self.book2.save()
            self.book2.authors.add(self.author2)
            self.book2.save()

            self.book3 = Book(id=103, name="book3", description="description3")
            self.book3.save()
            self.book3.authors.add(self.author1)
            self.book3.authors.add(self.author2)
            self.book3.save()

            self.order1 = Order(id=101,
                                user=self.user,
                                book=self.book1,
                                plated_end_at=TEST_DATE)
            self.order1.save()
            self.order2 = Order(id=102,
                                user=self.user,
                                book=self.book2,
                                plated_end_at=TEST_DATE)
            self.order2.save()
            self.order3 = Order(id=103,
                                user=self.user,
                                book=self.book3,
                                end_at=TEST_DATE_END,
                                plated_end_at=TEST_DATE)
            self.order3.save()

    def test__str__(self):
        """Test of the CustomUser.__str__() method"""
        book_returned = str(Book.objects.get(id=101))
        book_to_expect = "'id': 101, 'name': 'book1', 'description': 'description1', 'count': 1, 'authors': [101]"

        self.assertEqual(book_returned, book_to_expect)

    def test__repr__(self):
        """Test of the CustomUser.__repr__() method"""
        book_returned = repr(Book.objects.get(id=102))
        book_to_expect = "Book(id=102)"

        self.assertEqual(book_returned, book_to_expect)

    def test_get_by_id_positive(self):
        """Positive test of the CustomUser.get_by_id() method"""
        book = Book.get_by_id(101)
        self.assertEqual(book.id, 101)
        self.assertEqual(book.name, 'book1')
        self.assertEqual(book.description, "description1")
        self.assertEqual(book.count, 1)
        self.assertListEqual(list(book.authors.all()), [self.author1])

    def test_get_by_id_negative(self):
        """Negative test of the CustomUser.get_by_id() method"""
        book = Book.get_by_id(999)
        self.assertIsNone(book)

    def test_delete_by_id_positive(self):
        """ Test of the CustomUser.delete_by_id() method """
        self.assertTrue(Book.delete_by_id(103))
        self.assertRaises(Book.DoesNotExist, Book.objects.get, pk=103)
        self.assertRaises(Order.DoesNotExist, Order.objects.get, pk=103)
        self.assertEqual(self.author1, Author.objects.get(id=101))
        self.assertEqual(self.author2, Author.objects.get(id=102))

    def test_delete_by_id_negative(self):
        """ Test of the CustomUser.delete_by_id() method """
        self.assertFalse(Book.delete_by_id(999))

    def test_create_positive_name_description(self):
        """ Positive Test of the CustomUser.create method """

        book = Book.create(name="testBook", description="testDescription")
        self.assertIsInstance(book, Book)
        self.assertEqual(book.name, "testBook")
        self.assertEqual(book.description, "testDescription")
        self.assertEqual(book.count, 10)
        self.assertListEqual(list(book.authors.all()), [])

    def test_create_positive_name_description_empty(self):
        """ Positive Test of the CustomUser.create method """

        book = Book.create(name="testBook", description="")
        self.assertIsInstance(book, Book)
        self.assertEqual(book.name, "testBook")
        self.assertEqual(book.description, "")
        self.assertEqual(book.count, 10)
        self.assertListEqual(list(book.authors.all()), [])

    def test_create_positive_name_description_count(self):
        """ Positive Test of the CustomUser.create method """

        book = Book.create(name="testBook",
                           description="testDescription",
                           count=5)
        self.assertIsInstance(book, Book)
        self.assertEqual(book.name, "testBook")
        self.assertEqual(book.description, "testDescription")
        self.assertEqual(book.count, 5)
        self.assertListEqual(list(book.authors.all()), [])

    def test_create_positive_name_description_count_a(self):
        """ Positive Test of the CustomUser.create method """

        book = Book.create(name="testBook",
                           description="testDescription",
                           count=5,
                           authors=[self.author1, self.author2])
        self.assertIsInstance(book, Book)
        self.assertEqual(book.name, "testBook")
        self.assertEqual(book.description, "testDescription")
        self.assertEqual(book.count, 5)
        self.assertListEqual(list(book.authors.all()),
                             [self.author1, self.author2])

    def test_create_negative_len_name(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""
        book = Book.create(name="1" * 128, description="12")
        self.assertIsInstance(book, Book)
        book = Book.create(name="1" * 129, description="12")
        self.assertIsNone(book)

    def test_get_all(self):
        """ Positive Test of the CustomUser.create method TEST_DATE_END"""
        books = Book.get_all()
        books.sort(key=lambda book: book.id)
        self.assertListEqual(books, [self.book1, self.book2, self.book3])

    def test_add_authors(self):
        book = Book.objects.get(id=103)
        book.add_authors([self.author2])

        book = Book.objects.get(id=103)
        self.assertListEqual(list(book.authors.all()),
                             [self.author1, self.author2])

    def test_add_authors_duplicate(self):
        book = Book.objects.get(id=103)
        book.add_authors([self.author1, self.author2])

        book = Book.objects.get(id=103)
        self.assertListEqual(list(book.authors.all()),
                             [self.author1, self.author2])

    def test_remove_authors(self):
        book = Book.objects.get(id=103)
        book.remove_authors([self.author1, self.author2])

        book = Book.objects.get(id=103)
        self.assertListEqual(list(book.authors.all()), [])

    def test_update(self):
        book = Book.objects.get(id=101)
        book.update(name="testName", description="testDescription", count=5)
        book = Book.objects.get(id=101)
        self.assertIsInstance(book, Book)
        self.assertEqual(book.name, "testName")
        self.assertEqual(book.description, "testDescription")
        self.assertEqual(book.count, 5)
        self.assertListEqual(list(book.authors.all()), [self.author1])

    def test_update_name(self):
        book = Book.objects.get(id=101)
        book.update(name="testName")
        book = Book.objects.get(id=101)
        self.assertIsInstance(book, Book)
        self.assertEqual(book.name, "testName")
        self.assertEqual(book.description, "description1")
        self.assertEqual(book.count, 1)
        self.assertListEqual(list(book.authors.all()), [self.author1])