コード例 #1
0
    def setUpTestData(cls):
        cls.author_1 = create_author()
        cls.author_2 = create_author()

        cls.topics_by_author_1 = [
            create_topic(cls.author_1.id) for _ in range(2)
        ]
        cls.topics_by_author_2 = [
            create_topic(cls.author_2.id) for _ in range(2)
        ]

        cls.articles_by_author_1 = [
            create_article(author_id=cls.author_1.id,
                           topic_id=random.choice([
                               topic.pk for topic in cls.topics_by_author_2 +
                               cls.topics_by_author_1
                           ])) for _ in range(5)
        ]
        cls.articles_by_author_2 = [
            create_article(author_id=cls.author_2.id,
                           topic_id=random.choice([
                               topic.pk for topic in cls.topics_by_author_1 +
                               cls.topics_by_author_2
                           ])) for _ in range(5)
        ]
コード例 #2
0
    def test_author_registration_with_existing_username(self) -> None:
        """
        Makes a request to /api/authors/create/ to create a user but
        expects failure because we'll provide a username that already
        exists in the database by picking an Author randomly from db.
        """

        # Create a fake test Author
        author: Author = create_author()

        # Construct POST request with taken username
        response: Response = self.client.post(
            BASE_URL + '/create/', {
                'password': '******',
                'bio': fake.text(120),
                'email': fake.email(),
                'username': author.username,
                'first_name': fake.first_name(),
            })

        data: typing.Dict[typing.Any, typing.Any] = u.get_json(response)

        self.assertEqual(response.status_code, status.HTTP_409_CONFLICT)
        self.assertEqual(
            data, {'detail': f"User '{author.username}' already exists."})
コード例 #3
0
 def setUpTestData(cls) -> None:
     cls.author = create_author()
     cls.topic = create_topic(cls.author.pk)
     cls.articles: List[Article] = [
         create_article(topic_id=cls.topic.id,
                        author_id=cls.author.id,
                        draft=False) for _ in range(5)
     ]
コード例 #4
0
 def setUpTestData(cls) -> None:
     """
     Make a temporary author to check make authenticated responses with.
     """
     cls.author = create_author()
     cls.data = {
         'name': fake.text(45)[:-1],
         'description': fake.text(150),
         'thumbnail_url': 'https://picsum.photos/id/271/1900/1080',
     }
コード例 #5
0
    def setUpTestData(cls) -> None:
        """
        Creates a list of fake authors using author.tests.generators'
        generate_author() and compares the JSON result from AuthorListAPIView
        by testing valid and invalid authenticated requests. Also used
        for testing AuthorDetailAPIView that compares the author details.
        """

        # Define base url
        cls.url = BASE_URL + '/'

        # Make 9 "normal" authors.
        cls.authors: typing.List[Author] = [create_author() for _ in range(9)]

        # Make 1 superuser author.
        cls.super_author: Author = create_author(True)

        # Serialize data once so that it's not called in ever test
        cls.serialized_data = AuthorListSerializer(Author.objects.all(),
                                                   many=True).data
コード例 #6
0
 def setUpTestData(cls) -> None:
     cls.author = create_author()
     cls.author.verify()
     cls.topics = [create_topic(cls.author.pk) for _ in range(3)]
     cls.article_data = {
         'title': 'Hello World',
         'tags': 'hello,world',
         'content': lorem_ipsum.paragraphs(4),
         'topic_id': random.choice(cls.topics).id,
         'thumbnail_url': 'https://picsum.photos/id/'
                          f'{random.choice(THUMBNAIL_URL_IDs)}'
                          '/1900/1080/',
     }
コード例 #7
0
    def setUpTestData(cls) -> None:

        cls.authors: typing.List[typing.Tuple[int, Author]] = [
            (index, create_author()) for index in range(1, 3)
        ]

        # Create 4 topics, 2 by each author.
        cls.author_1_topics: typing.List[Topic] = [
            create_topic(cls.authors[0][1].pk) for _ in range(2)
        ]
        cls.author_2_topics: typing.List[Topic] = [
            create_topic(cls.authors[1][1].pk) for _ in range(2)
        ]
        cls.topics: typing.Set[Topic] = set(cls.author_1_topics + cls.author_2_topics)
コード例 #8
0
    def setUpTestData(cls) -> None:
        cls.author = create_author()

        cls.topics = [create_topic(cls.author.pk) for _ in range(3)]

        cls.articles = [create_article(
            draft=False,
            author_id=cls.author.id,
            topic_id=random.choice(cls.topics).id,
        ) for _ in range(5)]

        create_article(
            topic_id=random.choice(cls.topics).id,
            author_id=cls.author.id,
            draft=True
        )
コード例 #9
0
    def setUpTestData(cls) -> None:

        # Create authors.
        cls.author: Author = create_author()

        # Create topics.
        cls.topics: typing.List[Topic] = list(reversed([
            create_topic(cls.author.id) for _ in range(25)
        ]))

        # For later testing.
        cls.topic_1: Topic = random.choice(cls.topics)
        cls.topic_2: Topic = random.choice(cls.topics)

        kwargs = {'draft': False, 'author_id': cls.author.id}

        cls.articles_for_topic_1 = [create_article(topic_id=cls.topic_1.id, **kwargs) for _ in range(5)]
        cls.articles_for_topic_2 = [create_article(topic_id=cls.topic_2.id, **kwargs) for _ in range(5)]