def test_success_validate_oauth_and_get_access_token(self): self.oauth_client = OauthClient.objects.create( client_name='test', client_public_key='saa', client_secret_key='saa', callback_url='http://test2.dev', is_enabled='Y', created_at=get_today_date(), updated_at=get_today_date()) self.user = User.objects.create(name='test', email='*****@*****.**', created_at=get_today_date(), updated_at=get_today_date()) self.oauth_grant = OauthGrant.objects.create( grant_code='bb', client=self.oauth_client, user=self.user, ip_address='127.0.0.1', created_at=get_today_date(), ) response = self.client.post('/api/v1/oauth/', { 'client_key': 'saa', 'secret_key': 'saa', 'grant_code': 'bb' }) body_response = response.json() self.assertEqual(response.status_code, 200) self.assertEqual(body_response['status'], 200) self.assertEqual(body_response['success'], True) self.assertEqual(body_response['data']['type'], 'bearer')
def create_oauth_access_token(user, client): try: return OauthAccessToken.objects.create( access_token=random_string(), user=user, client=client, refresh_token=random_string(), expired_at=add_days_from_today(20), created_at=get_today_date(), updated_at=get_today_date()) except Exception as e: return None
def create_oauth_grant(grant_code, client=None, user=None, ip_address=None): try: return OauthGrant.objects.create(grant_code=str(grant_code), client=client, user=user, ip_address=str(ip_address), created_at=get_today_date(), expired_at=add_minutes_from_now( settings.GRANT_MINUTES)) except Exception as e: return None
def test_success_login(self): self.user = User.objects.create(name='tes', email='*****@*****.**', password=encrypt_password(123456), confirmed_account='Y', created_at=get_today_date(), updated_at=get_today_date()) self.oauth_client = OauthClient.objects.create( client_name='tes', client_public_key='aaa', client_secret_key='bbb', callback_url='http://aa.dev', is_enabled='Y', created_at=get_today_date(), updated_at=get_today_date()) response = self.client.post('/?redirect_url=http://aa.dev', { 'email': self.user.email, 'password': 123456 }) self.assertEqual(response.status_code, 302)
def test_invalid_grant_code(self): self.oauth_client = OauthClient.objects.create( client_name='test', client_public_key='sa', client_secret_key='sa', callback_url='http://test.dev', is_enabled='Y', created_at=get_today_date(), updated_at=get_today_date()) response = self.client.post('/api/v1/oauth/', { 'client_key': 'sa', 'secret_key': 'sa', 'grant_code': 'bb' }) body_response = response.json() self.assertEqual(response.status_code, 400) self.assertEqual(body_response['error'], 'INVALID_GRANT') self.assertEqual(body_response['status'], 400) self.assertEqual(body_response['success'], False)