def test_user_can_login_with_device(self):
user = get_user_model().objects.create_user('test', email='test', password='test')
client = APIClient()
response = client.post(reverse('auth-login'), {'username': 'test', 'password': 'test', 'device_id': 'device-1'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
auth_token = AuthToken.objects.get(user=user, device_id='device-1')
data = json.loads(response.content.decode('utf-8'))
token = data['token']
self.assertEqual(token, auth_token.key)
# Same device ID should return same token
response = client.post(reverse('auth-login'), {'username': 'test', 'password': 'test', 'device_id': 'device-1'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = json.loads(response.content.decode('utf-8'))
token = data['token']
self.assertEqual(token, auth_token.key)
# Different device ID should return different token
response = client.post(reverse('auth-login'), {'username': 'test', 'password': 'test', 'device_id': 'device-2'})
self.assertEqual(response.status_code, status.HTTP_200_OK)
auth_token = AuthToken.objects.get(user=user, device_id='device-2')
data = json.loads(response.content.decode('utf-8'))
token = data['token']
self.assertEqual(token, auth_token.key)
评论列表
文章目录