# tests/unit/services/test_admin_service.py import pytest from fastapi import HTTPException from app.services.admin_service import AdminService from models.database_models import MarketplaceImportJob, Shop @pytest.mark.unit @pytest.mark.admin class TestAdminService: """Test suite for AdminService following the application's testing patterns""" def setup_method(self): """Setup method following the same pattern as product service tests""" self.service = AdminService() def test_get_all_users(self, db, test_user, test_admin): """Test getting all users with pagination""" users = self.service.get_all_users(db, skip=0, limit=10) assert len(users) >= 2 # test_user + test_admin user_ids = [user.id for user in users] assert test_user.id in user_ids assert test_admin.id in user_ids def test_get_all_users_with_pagination(self, db, test_user, test_admin): """Test user pagination works correctly""" users = self.service.get_all_users(db, skip=0, limit=1) assert len(users) == 1 users_second_page = self.service.get_all_users(db, skip=1, limit=1) assert len(users_second_page) == 1 assert users[0].id != users_second_page[0].id def test_toggle_user_status_deactivate(self, db, test_user, test_admin): """Test deactivating a user""" assert test_user.is_active is True user, message = self.service.toggle_user_status(db, test_user.id, test_admin.id) assert user.id == test_user.id assert user.is_active is False assert f"{user.username} has been deactivated" in message def test_toggle_user_status_activate(self, db, test_user, test_admin): """Test activating a user""" # First deactivate the user test_user.is_active = False db.commit() user, message = self.service.toggle_user_status(db, test_user.id, test_admin.id) assert user.id == test_user.id assert user.is_active is True assert f"{user.username} has been activated" in message def test_toggle_user_status_user_not_found(self, db, test_admin): """Test toggle user status when user not found""" with pytest.raises(HTTPException) as exc_info: self.service.toggle_user_status(db, 99999, test_admin.id) assert exc_info.value.status_code == 404 assert "User not found" in str(exc_info.value.detail) def test_toggle_user_status_cannot_deactivate_self(self, db, test_admin): """Test that admin cannot deactivate their own account""" with pytest.raises(HTTPException) as exc_info: self.service.toggle_user_status(db, test_admin.id, test_admin.id) assert exc_info.value.status_code == 400 assert "Cannot deactivate your own account" in str(exc_info.value.detail) def test_get_all_shops(self, db, test_shop): """Test getting all shops with total count""" shops, total = self.service.get_all_shops(db, skip=0, limit=10) assert total >= 1 assert len(shops) >= 1 shop_codes = [shop.shop_code for shop in shops] assert test_shop.shop_code in shop_codes def test_verify_shop_mark_verified(self, db, test_shop): """Test marking shop as verified""" # Ensure shop starts unverified test_shop.is_verified = False db.commit() shop, message = self.service.verify_shop(db, test_shop.id) assert shop.id == test_shop.id assert shop.is_verified is True assert f"{shop.shop_code} has been verified" in message def test_verify_shop_not_found(self, db): """Test verify shop when shop not found""" with pytest.raises(HTTPException) as exc_info: self.service.verify_shop(db, 99999) assert exc_info.value.status_code == 404 assert "Shop not found" in str(exc_info.value.detail) def test_get_marketplace_import_jobs_no_filters(self, db, test_marketplace_job): """Test getting marketplace import jobs without filters using fixture""" result = self.service.get_marketplace_import_jobs(db, skip=0, limit=10) assert len(result) >= 1 # Find our test job in the results test_job = next( (job for job in result if job.job_id == test_marketplace_job.id), None ) assert test_job is not None assert test_job.marketplace == test_marketplace_job.marketplace assert test_job.shop_name == test_marketplace_job.shop_name assert test_job.status == test_marketplace_job.status