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)
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)
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)
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)
def test_can_sign_up(self): chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**', 'badpass') assert chirper.name == 'Nate' assert chirper.username == 'natec425' assert chirper.email == '*****@*****.**'
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})
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")
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()
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())
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)
def middleware(request: HttpRequest) -> HttpResponse: try: data = json.loads(request.body.decode('utf-8')) key = data['key'] request.user = ChirperUser.find_by_key(key) except (json.JSONDecodeError, KeyError, ChirperUser.DoesNotExist): request.user = AnonymousUser() except Exception as e: print(e) # TODO: Report error to sentry request.user = AnonymousUser() finally: return get_response(request)
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)
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())
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)
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())
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 }] })
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')
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.'] } })
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': [] })
def feed(request: HttpRequest, username: str) -> HttpResponse: try: chirper = ChirperUser.find_by_username(username) except ChirperUser.DoesNotExist: return JsonResponse({}, HTTPStatus.NOT_FOUND) paginator = Paginator(chirper.feed(), 25) page = request.GET.get('page') try: chirps = paginator.page(page) except PageNotAnInteger: chirps = paginator.page(1) except EmptyPage: chirps = paginator.page(paginator.num_pages) return JsonResponse( { 'chirper': { 'name': chirper.name, 'username': chirper.username, 'description': chirper.description, 'location': chirper.location, 'website': chirper.website, 'joined': { 'month': chirper.joined.month, 'year': chirper.joined.year } }, 'chirps': [{ 'author': { 'name': c.author.name, 'username': c.author.username }, 'date': { 'month': c.date.month, 'day': c.date.day, 'year': c.date.year }, 'message': c.message } for c in chirps] }, 200)
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())
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)
def test_username_does_exist(self): chirper = ChirperUser.signup('Nate', 'natec425', '*****@*****.**', 'badpass') self.assertTrue(ChirperUser.username_exists('natec425')) self.assertFalse(ChirperUser.username_exists('notnate'))
def test_username_cannot_have_at_sign(self): with self.assertRaises(ValidationError): ChirperUser.signup('Nate', '@natec425', '*****@*****.**', 'badpass')
def username_exists(request, username): return JsonResponse({'exists': ChirperUser.username_exists(username)})