Пример #1
0
    def test_cannot_signup_user_with_long_name(self):
        with self.assertRaises(ValidationError):
            ChirperUser.signup('N' * 200, 'natec425', '*****@*****.**',
                               'badpass')

        self.assertEqual(User.objects.count(), 0)
        self.assertEqual(ChirperUser.objects.count(), 0)
Пример #2
0
    def test_cannot_signup_two_users_with_same_username(self):
        ChirperUser.signup('Nate', 'natec425', '*****@*****.**', 'badpass')

        with self.assertRaises(ValidationError):
            ChirperUser.signup('Not Nate', 'natec425', '*****@*****.**',
                               'badpass2')

        self.assertEqual(User.objects.count(), 1)
        self.assertEqual(ChirperUser.objects.count(), 1)
Пример #3
0
    def test_feed_with_chirping_at(self):
        nate = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                  'badpass')

        not_nate = ChirperUser.signup('Not Nate', 'not_nate',
                                      '*****@*****.**', 'badpass')

        hey_chirp = nate.chirp('Hey @not_nate')

        self.assertQuerysetEqual(nate.feed(), [hey_chirp], transform=identity)
        self.assertQuerysetEqual(not_nate.feed(), [hey_chirp],
                                 transform=identity)
Пример #4
0
    def test_chirp_chirping_at(self):
        nate = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                  'badpass')

        not_nate = ChirperUser.signup('Not Nate', 'not_nate',
                                      '*****@*****.**', 'badpass')

        chirp = nate.chirp('Hello @not_nate @not_nate @not_nate')

        self.assertQuerysetEqual(chirp.chirping_at.all(), [not_nate],
                                 transform=identity)

        self.assertQuerysetEqual(not_nate.feed(), [chirp], transform=identity)

        self.assertQuerysetEqual(nate.feed(), [chirp], transform=identity)
Пример #5
0
    def test_can_sign_up(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        assert chirper.name == 'Nate'
        assert chirper.username == 'natec425'
        assert chirper.email == '*****@*****.**'
Пример #6
0
    def test_chirper_user_can_chirp(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        chirp = chirper.chirp("Hello World")

        self.assertEqual(chirp.author, chirper)
        self.assertEqual(chirp.message, "Hello World")
Пример #7
0
    def test_username_exists(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        response = self.client.get('/api/username_exists/natec425/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.json(), {'exists': True})
Пример #8
0
    def test_login_logout(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        self.assertFalse(chirper.is_logged_in())
        chirper.login()
        self.assertTrue(chirper.is_logged_in())
        chirper.logout()
        self.assertFalse(chirper.is_logged_in())
Пример #9
0
    def test_login_logout_multiple_times(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        for _ in range(5):
            self.assertFalse(chirper.is_logged_in())
            chirper.login()
            self.assertTrue(chirper.is_logged_in())
            chirper.logout()
Пример #10
0
    def test_feed_with_chirps(self):

        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        hello_chirp = chirper.chirp("Hello World")
        game_over_chirp = chirper.chirp("Game Over")

        self.assertQuerysetEqual(chirper.feed(),
                                 [game_over_chirp, hello_chirp],
                                 transform=identity)
Пример #11
0
    def test_chirp_without_message(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        chirper.login()

        response = self.client.post(
            '/api/chirp/',
            json.dumps({'key': chirper.session.key}),
            content_type='application/json',
        )

        self.assertEqual(response.status_code, 422)
Пример #12
0
    def test_chirp_with_malformed_payload(self):

        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        chirper.login()

        response = self.client.post(
            '/api/chirp/',
            'this',
            content_type='text/plain',
        )

        self.assertEqual(response.status_code, 400)
Пример #13
0
    def test_successful_login(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        response = self.client.post('/api/login/',
                                    json.dumps({
                                        'username': '******',
                                        'password': '******'
                                    }),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 201)
        self.assertIn('key', json.loads(response.content.decode('utf-8')))
        self.assertTrue(chirper.is_logged_in())
Пример #14
0
    def test_invalid_password_login(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        response = self.client.post('/api/login/',
                                    json.dumps({
                                        'username': '******',
                                        'password': '******'
                                    }),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 401)
        self.assertEqual(response.json(),
                         {'error': 'INVALID_USERNAME_PASSWORD'})

        self.assertFalse(chirper.is_logged_in())
Пример #15
0
    def test_nonempty_feed(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')
        hello_chirp = chirper.chirp("Hello")
        world_chirp = chirper.chirp("World")

        response = self.client.get('/api/natec425/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.json(), {
                'chirper': {
                    'name': 'Nate',
                    'username': '******',
                    'joined': {
                        'month': chirper.joined.month,
                        'year': chirper.joined.year
                    },
                    'description': '',
                    'location': '',
                    'website': ''
                },
                'chirps': [{
                    'author': {
                        'name': chirper.name,
                        'username': chirper.username
                    },
                    'date': {
                        'month': world_chirp.date.month,
                        'day': world_chirp.date.day,
                        'year': world_chirp.date.year
                    },
                    'message': world_chirp.message
                }, {
                    'author': {
                        'name': chirper.name,
                        'username': chirper.username
                    },
                    'date': {
                        'month': hello_chirp.date.month,
                        'day': hello_chirp.date.day,
                        'year': hello_chirp.date.year
                    },
                    'message': hello_chirp.message
                }]
            })
Пример #16
0
    def test_chirp_with_logged_in_user(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        chirper.login()

        response = self.client.post(
            '/api/chirp/',
            json.dumps({
                'key': chirper.session.key,
                'message': 'Hello World'
            }),
            content_type='application/json',
        )

        self.assertEqual(response.status_code, 201)
        self.assertEqual(chirper.chirp_set.count(), 1)
        self.assertEqual(chirper.chirp_set.first().message, 'Hello World')
Пример #17
0
    def test_duplicate_username_signup(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        response = self.client.post('/api/signup/',
                                    json.dumps({
                                        'name': 'Not Nate',
                                        'username': '******',
                                        'email': '*****@*****.**',
                                        'password': '******'
                                    }),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 422)
        self.assertEqual(
            response.json(), {
                'error': 'INVALID_DATA',
                'errors': {
                    'username': ['A user with that username already exists.']
                }
            })
Пример #18
0
    def test_empty_feed(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')
        response = self.client.get('/api/natec425/')

        self.assertEqual(response.status_code, 200)
        self.assertEqual(
            response.json(), {
                'chirper': {
                    'name': 'Nate',
                    'username': '******',
                    'joined': {
                        'month': chirper.joined.month,
                        'year': chirper.joined.year
                    },
                    'description': '',
                    'location': '',
                    'website': ''
                },
                'chirps': []
            })
Пример #19
0
    def test_login_then_logout(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        response = self.client.post('/api/login/',
                                    json.dumps({
                                        'username': '******',
                                        'password': '******'
                                    }),
                                    content_type='application/json')

        self.assertEqual(response.status_code, 201)
        self.assertTrue(chirper.is_logged_in())

        response = self.client.post('/api/logout/',
                                    response.content.decode('utf-8'),
                                    content_type='application/json')

        chirper.refresh_from_db()

        self.assertEqual(response.status_code, 200)
        self.assertFalse(chirper.is_logged_in())
Пример #20
0
def signup(request: HttpRequest) -> HttpResponse:
    '''Signs up a new user.

    It expects a json payload with the following fields:
        - name
        - username
        - email
        - password

    Success Response:
        201, {}

    Failure Responses:
        400, {error: "MALFORMED_REQUEST"}
        422, {error: "INVALID_DATA", errors: <ValidationErrors>}
    '''
    try:
        data = json.loads(request.body.decode('utf-8'))
        name = data.get('name')
        username = data.get('username')
        email = data.get('email')
        password = data.get('password')
    except json.JSONDecodeError:
        return JsonResponse({'error': 'MALFORMED_REQUEST'},
                            HTTPStatus.BAD_REQUEST)

    try:
        chirper = ChirperUser.signup(name, username, email, password)
    except ValidationError as e:
        return JsonResponse({
            'error': 'INVALID_DATA',
            'errors': e.message_dict
        }, HTTPStatus.UNPROCESSABLE_ENTITY)

    chirper.login()

    return JsonResponse({'key': chirper.session.key}, HTTPStatus.CREATED)
Пример #21
0
    def test_username_does_exist(self):
        chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**',
                                     'badpass')

        self.assertTrue(ChirperUser.username_exists('natec425'))
        self.assertFalse(ChirperUser.username_exists('notnate'))
Пример #22
0
 def test_username_cannot_have_at_sign(self):
     with self.assertRaises(ValidationError):
         ChirperUser.signup('Nate', '@natec425', '*****@*****.**',
                            'badpass')