""" Integration tests for Auth module endpoints Tests login, register, user management return unified format """ from rest_framework import status from django.test import TestCase from rest_framework.test import APIClient from igny8_core.auth.models import User, Account, Plan class AuthIntegrationTestCase(TestCase): """Integration tests for Auth module""" def setUp(self): """Set up test fixtures""" self.client = APIClient() # Create test plan and account self.plan = Plan.objects.create( name="Test Plan", slug="test-plan", price=0, credits_per_month=1000 ) # Create test user first (Account needs owner) self.user = User.objects.create_user( username='testuser', email='test@test.com', password='testpass123', role='owner' ) # Create test account with owner self.account = Account.objects.create( name="Test Account", slug="test-account", plan=self.plan, owner=self.user ) # Update user to have account self.user.account = self.account self.user.save() def assert_unified_response_format(self, response, expected_success=True): """Assert response follows unified format""" self.assertIn('success', response.data) self.assertEqual(response.data['success'], expected_success) if expected_success: self.assertTrue('data' in response.data or 'results' in response.data) else: self.assertIn('error', response.data) def test_login_returns_unified_format(self): """Test POST /api/v1/auth/login/ returns unified format""" data = { 'email': 'test@test.com', 'password': 'testpass123' } response = self.client.post('/api/v1/auth/login/', data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assert_unified_response_format(response, expected_success=True) self.assertIn('data', response.data) self.assertIn('user', response.data['data']) self.assertIn('access', response.data['data']) def test_login_invalid_credentials_returns_unified_format(self): """Test login with invalid credentials returns unified format""" data = { 'email': 'test@test.com', 'password': 'wrongpassword' } response = self.client.post('/api/v1/auth/login/', data, format='json') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assert_unified_response_format(response, expected_success=False) self.assertIn('error', response.data) self.assertIn('request_id', response.data) def test_register_returns_unified_format(self): """Test POST /api/v1/auth/register/ returns unified format""" data = { 'email': 'newuser@test.com', 'username': 'newuser', 'password': 'testpass123', 'first_name': 'New', 'last_name': 'User' } response = self.client.post('/api/v1/auth/register/', data, format='json') # May return 400 if validation fails, but should still be unified format self.assertIn(response.status_code, [status.HTTP_201_CREATED, status.HTTP_400_BAD_REQUEST]) self.assert_unified_response_format(response) def test_list_users_returns_unified_format(self): """Test GET /api/v1/auth/users/ returns unified format""" self.client.force_authenticate(user=self.user) response = self.client.get('/api/v1/auth/users/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assert_paginated_response(response) def test_list_accounts_returns_unified_format(self): """Test GET /api/v1/auth/accounts/ returns unified format""" self.client.force_authenticate(user=self.user) response = self.client.get('/api/v1/auth/accounts/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assert_paginated_response(response) def test_list_sites_returns_unified_format(self): """Test GET /api/v1/auth/sites/ returns unified format""" self.client.force_authenticate(user=self.user) response = self.client.get('/api/v1/auth/sites/') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assert_paginated_response(response) def test_unauthorized_returns_unified_format(self): """Test 401 errors return unified format""" # Don't authenticate response = self.client.get('/api/v1/auth/users/') self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) self.assert_unified_response_format(response, expected_success=False) self.assertIn('error', response.data) self.assertIn('request_id', response.data)