Exemple #1
0
    def test_should_read_thought(self):
        user = UserBuilder().with_first_name('Bren') \
                                  .with_last_name('Magro') \
                                  .with_username('breninho') \
                                  .with_email('*****@*****.**') \
                                  .with_password('123456') \
                                  .build()
        user.save()
        thought = ThoughtBuilder().with_thought('Adipiscing ipsum dolor sit.')\
                                  .with_owner(user)\
                                  .build()
        thought.save()

        response = self.client.get(reverse('thought-detail',
                                           kwargs={'pk': thought.id}),
                                   {'username': user.username},
                                   format='json')

        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assert_response_thought(
            {
                'thought': 'Adipiscing ipsum dolor sit.',
                'user': {
                    'username': user.username
                }
            }, response.data)
Exemple #2
0
    def test_should_publish_a_thought(self):
        user = UserBuilder().with_first_name('Bren') \
                            .with_last_name('Magro') \
                            .with_username('breninho') \
                            .with_email('*****@*****.**') \
                            .with_password('123456') \
                            .build()
        user.save()
        self.authenticate_user(user, '123456')
        data = {
            'thought':
            '''Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in lectus vitae elit congue interdum sed ut quam. Vivamus at lectus lacus. Proin fringilla porta sapien, vel scelerisque quam aliquet at. Integer faucibus justo nibh, sodales placerat eros consectetur sed. Praesent ultrices felis arcu, at luctus turpis auctor fermentum.'''
        }

        url = reverse('thought-list')
        self.client.request()
        response = self.client.post(url, data, format='json')

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)
        self.assertEqual(1, Thought.objects.count())
        expected_thought = {
            'thought': data['thought'],
            'user': {
                'username': user.username
            }
        }
        self.assert_response_thought(expected_thought, response.data)
        thought = Thought.objects.get()
        self.assertEqual(expected_thought['thought'], thought.thought)
        self.assertEqual(expected_thought['user']['username'],
                         thought.owner.username)
Exemple #3
0
    def test_should_get_user_info(self):
        user = UserBuilder().with_username('breno')\
                            .with_password('123456')\
                            .with_first_name('Breno')\
                            .with_last_name('Magro')\
                            .with_email('*****@*****.**')\
                            .build()
        user.save()
        url = reverse('user-detail', kwargs={'pk': user.id})
        self.authenticate_user(user, '123456')

        response = self.client.get(url, format='json')
        response_json = json.loads(response.rendered_content.decode())
        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assert_response_user(user, response_json)
Exemple #4
0
    def test_should_not_obtain_token_if_wrong_credentials(self):
        user = UserBuilder().with_username('breno')\
                            .with_password('123456')\
                            .with_first_name('Breno')\
                            .with_last_name('Magro')\
                            .with_email('*****@*****.**')\
                            .build()
        user.save()

        credentials = {
            'username': '******',
            'password': '******'
        }
        url = reverse('token_obtain_pair')
        response = self.client.post(url, credentials, format='json')

        self.assertEqual(status.HTTP_401_UNAUTHORIZED, response.status_code)
Exemple #5
0
    def test_should_no_publish_a_thought_bigger_than_800_characters(self):
        user = UserBuilder().with_first_name('Bren') \
                            .with_last_name('Magro') \
                            .with_username('breninho') \
                            .with_email('*****@*****.**') \
                            .with_password('123456') \
                            .build()
        user.save()
        self.authenticate_user(user, '123456')
        data = {
            'thought':
            'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in lectus vitae elit congue interdum sed ut quam. Vivamus at lectus lacus. Proin fringilla porta sapien, vel scelerisque quam aliquet at. Integer faucibus justo nibh, sodales placerat eros consectetur sed. Praesent ultrices felis arcu, at luctus turpis auctor fermentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in lectus vitae elit congue interdum sed ut quam. Vivamus at lectus lacus. Proin fringilla porta sapien, vel scelerisque quam aliquet at. Integer faucibus justo nibh, sodales placerat eros consectetur sed. Praesent ultrices felis arcu, at luctus turpis auctor fermentum. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus in lectus vitae elit congue interdum sed ut quam. Vivamus at lectus lacus. Proin fringilla porta sapien, vel scelerisque quam aliquet at. Integer faucibus justo nibh, sodales placerat eros consectetur sed. Praesent ultrices felis arcu, at luctus turpis auctor fermentum.'
        }
        url = reverse('thought-list')

        response = self.client.post(url, data, format='json')

        self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
        self.assertEqual(0, Thought.objects.count())
Exemple #6
0
    def test_should_not_register_a_new_user_if_email_in_use(self):
        first_user = UserBuilder().with_username('breno')\
                                  .with_password('123456')\
                                  .with_first_name('Breno')\
                                  .with_last_name('Magro')\
                                  .with_email('*****@*****.**')\
                                  .build()
        first_user.save()
        second_user_data = UserBuilder().with_username('breno2')\
                                        .with_password('123456')\
                                        .with_first_name('Breno2')\
                                        .with_last_name('Magro2')\
                                        .with_email('*****@*****.**')\
                                        .build(json=True)

        url = reverse('user-list')
        response = self.client.post(url, second_user_data, format='json')

        self.assertEqual(status.HTTP_400_BAD_REQUEST, response.status_code)
Exemple #7
0
    def test_should_obtain_pair_token(self):
        user = UserBuilder().with_username('breno')\
                            .with_password('123456')\
                            .with_first_name('Breno')\
                            .with_last_name('Magro')\
                            .with_email('*****@*****.**')\
                            .build()
        user.save()

        url = reverse('token_obtain_pair')
        credentials = {
            'username': '******',
            'password': '******'
        }
        response = self.client.post(url, credentials, format='json')

        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assert_refresh_token(response.data['refresh'])
        self.assert_access_token(response.data['access'])
Exemple #8
0
    def test_should_list_user_thoughts(self):
        first_user = UserBuilder().with_first_name('Bren') \
                                  .with_last_name('Magro') \
                                  .with_username('breninho') \
                                  .with_email('*****@*****.**') \
                                  .with_password('123456') \
                                  .build()
        first_user.save()
        second_user = UserBuilder().with_first_name('Extra')\
                                  .with_last_name('User')\
                                  .with_username('extra')\
                                  .with_email('*****@*****.**')\
                                  .with_password('123456')\
                                  .build()
        second_user.save()

        thought = ThoughtBuilder().with_thought('Adipiscing ipsum dolor sit.')\
                                  .with_owner(first_user)\
                                  .build()
        thought.save()
        thought = ThoughtBuilder().with_thought('Lorem ipsum dolor sit.')\
                                  .with_owner(second_user)\
                                  .build()
        thought.save()
        thought = ThoughtBuilder().with_thought('consectetur adipiscing.')\
                                  .with_owner(second_user)\
                                  .build()
        thought.save()

        response = self.client.get(reverse('thought-list'),
                                   {'username': second_user.username},
                                   format='json')

        self.assertEqual(status.HTTP_200_OK, response.status_code)
        self.assertEqual(len(response.data['results']), 2)
        self.assert_response_thought(
            {
                'thought': 'Lorem ipsum dolor sit.',
                'user': {
                    'username': second_user.username
                }
            }, response.data['results'][0])
        self.assert_response_thought(
            {
                'thought': 'consectetur adipiscing.',
                'user': {
                    'username': second_user.username
                }
            }, response.data['results'][1])
Exemple #9
0
    def test_should_publish_a_thought_with_hashtags_that_already_exists(self):
        user = UserBuilder().with_first_name('Bren') \
                            .with_last_name('Magro') \
                            .with_username('breninho') \
                            .with_email('*****@*****.**') \
                            .with_password('123456') \
                            .build()
        user.save()
        hashtag = Hashtag(hashtag='FelisArcu', creator=user)
        hashtag.save()
        self.authenticate_user(user, '123456')
        data = {'thought': '''Lorem ipsum dolor sit. #FelisArcu #atluctus.'''}

        url = reverse('thought-list')
        self.client.request()
        response = self.client.post(url, data, format='json')

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)
        self.assertEqual(1, Thought.objects.count())
        expected_thought = {
            'thought': data['thought'],
            'user': {
                'username': user.username
            }
        }
        self.assert_response_thought(expected_thought, response.data)
        thought = Thought.objects.get()
        self.assertEqual(expected_thought['thought'], thought.thought)
        self.assertEqual(expected_thought['user']['username'],
                         thought.owner.username)

        hashtags = Hashtag.objects.filter(thoughts=thought)
        self.assertEqual(len(hashtags), 2)
        self.assertEqual(hashtags[0].hashtag, 'FelisArcu')
        self.assertEqual(hashtags[0].creator, thought.owner)
        self.assertEqual(hashtags[0].thoughts.all()[0], thought)
        self.assertEqual(hashtags[1].hashtag, 'atluctus')
        self.assertEqual(hashtags[1].creator, thought.owner)
        self.assertEqual(hashtags[1].thoughts.all()[0], thought)
Exemple #10
0
    def test_should_register_new_user(self):
        user_data = UserBuilder().with_username('breno')\
                                 .with_password('123456')\
                                 .with_first_name('Breno')\
                                 .with_last_name('Magro')\
                                 .with_email('*****@*****.**')\
                                 .build(json=True)
        url = reverse('user-list')

        response = self.client.post(url, user_data, format='json')
        response_json = json.loads(response.rendered_content.decode())

        self.assertEqual(status.HTTP_201_CREATED, response.status_code)
        self.assert_response_user(user_data, response_json)
        persisted_user = User.objects.get()
        self.assert_persisted_user(user_data, persisted_user)
Exemple #11
0
    def test_authenticated_user_should_not_see_info_from_another_user(self):
        user = UserBuilder().with_username('breno')\
                            .with_password('123456')\
                            .with_first_name('Breno')\
                            .with_last_name('Magro')\
                            .with_email('*****@*****.**')\
                            .build()
        user.save()
        self.authenticate_user(user, '123456')
        other_user = UserBuilder().with_username('breno2')\
                                  .with_password('123456')\
                                  .with_first_name('Breno2')\
                                  .with_last_name('Magro2')\
                                  .with_email('*****@*****.**')\
                                  .build()
        other_user.save()

        url = reverse('user-detail', kwargs={'pk': other_user.id})
        response = self.client.get(url, format='json')

        self.assertEqual(status.HTTP_403_FORBIDDEN, response.status_code)