|
1 | 1 | # test_organization.py
|
2 | 2 |
|
3 |
| -import pytest |
4 |
| -from utils.models import Organization |
5 |
| - |
6 |
| -def test_organization_creation(): |
7 |
| - """Test basic organization creation""" |
8 |
| - org = Organization("Test Org", "Test Description") |
9 |
| - assert org.name == "Test Org" |
10 |
| - assert org.description == "Test Description" |
11 |
| - assert org.roles == [] |
12 |
| - assert org.members == [] |
13 |
| - |
14 |
| -def test_organization_add_role(): |
15 |
| - """Test adding a role to organization""" |
16 |
| - org = Organization("Test Org", "Test Description") |
17 |
| - role = {"name": "Admin", "permissions": ["read", "write"]} |
18 |
| - org.add_role(role) |
19 |
| - assert len(org.roles) == 1 |
20 |
| - assert org.roles[0] == role |
21 |
| - |
22 |
| -def test_organization_add_member(): |
23 |
| - """Test adding a member to organization""" |
24 |
| - org = Organization("Test Org", "Test Description") |
25 |
| - member = {"id": 1, "name": "John Doe"} |
26 |
| - org.add_member(member) |
27 |
| - assert len(org.members) == 1 |
28 |
| - assert org.members[0] == member |
29 |
| - |
30 |
| -def test_organization_remove_member(): |
31 |
| - """Test removing a member from organization""" |
32 |
| - org = Organization("Test Org", "Test Description") |
33 |
| - member = {"id": 1, "name": "John Doe"} |
34 |
| - org.add_member(member) |
35 |
| - org.remove_member(1) |
36 |
| - assert len(org.members) == 0 |
37 |
| - |
38 |
| -def test_organization_get_member(): |
39 |
| - """Test getting a member from organization""" |
40 |
| - org = Organization("Test Org", "Test Description") |
41 |
| - member = {"id": 1, "name": "John Doe"} |
42 |
| - org.add_member(member) |
43 |
| - retrieved_member = org.get_member(1) |
44 |
| - assert retrieved_member == member |
45 |
| - |
46 |
| -def test_organization_get_nonexistent_member(): |
47 |
| - """Test getting a non-existent member""" |
48 |
| - org = Organization("Test Org", "Test Description") |
49 |
| - assert org.get_member(1) is None |
50 |
| - |
51 |
| -# Additional tests for organization.py |
52 |
| - |
53 |
| -def test_organization_invalid_name(): |
54 |
| - """Test organization creation with invalid name""" |
55 |
| - with pytest.raises(ValueError): |
56 |
| - Organization("", "Description") |
57 |
| - |
58 |
| -def test_organization_none_name(): |
59 |
| - """Test organization creation with None name""" |
60 |
| - with pytest.raises(ValueError): |
61 |
| - Organization(None, "Description") |
| 3 | +from utils.models import Organization, Role |
| 4 | +from sqlmodel import select |
| 5 | + |
| 6 | +def test_create_organization_success(auth_client, session, test_user): |
| 7 | + """Test successful organization creation""" |
| 8 | + response = auth_client.post( |
| 9 | + "/organizations/create", |
| 10 | + data={"name": "New Test Organization"}, |
| 11 | + follow_redirects=False |
| 12 | + ) |
| 13 | + |
| 14 | + # Check response |
| 15 | + assert response.status_code == 303 # Redirect status code |
| 16 | + assert "/organizations/" in response.headers["location"] |
| 17 | + |
| 18 | + # Verify database state |
| 19 | + org = session.exec( |
| 20 | + select(Organization) |
| 21 | + .where(Organization.name == "New Test Organization") |
| 22 | + ).first() |
| 23 | + |
| 24 | + assert org is not None |
| 25 | + assert org.name == "New Test Organization" |
| 26 | + |
| 27 | + # Verify default roles were created |
| 28 | + roles = session.exec( |
| 29 | + select(Role) |
| 30 | + .where(Role.organization_id == org.id) |
| 31 | + ).all() |
| 32 | + |
| 33 | + assert len(roles) > 0 |
| 34 | + assert any(role.name == "Owner" for role in roles) |
| 35 | + |
| 36 | + # Verify test_user was assigned as owner |
| 37 | + owner_role = next(role for role in roles if role.name == "Owner") |
| 38 | + assert test_user in owner_role.users |
| 39 | + |
| 40 | +def test_create_organization_empty_name(auth_client): |
| 41 | + """Test organization creation with empty name""" |
| 42 | + response = auth_client.post( |
| 43 | + "/organizations/create", |
| 44 | + data={"name": " "} # Empty or whitespace name |
| 45 | + ) |
| 46 | + |
| 47 | + assert response.status_code == 400 |
| 48 | + assert "Organization name cannot be empty" in response.text |
| 49 | + |
| 50 | +def test_create_organization_duplicate_name(auth_client, test_organization): |
| 51 | + """Test organization creation with duplicate name""" |
| 52 | + response = auth_client.post( |
| 53 | + "/organizations/create", |
| 54 | + data={"name": test_organization.name} |
| 55 | + ) |
| 56 | + |
| 57 | + assert response.status_code == 400 |
| 58 | + assert "Organization name already taken" in response.text |
| 59 | + |
| 60 | +def test_create_organization_unauthenticated(unauth_client): |
| 61 | + """Test organization creation without authentication""" |
| 62 | + response = unauth_client.post( |
| 63 | + "/organizations/create", |
| 64 | + data={"name": "Unauthorized Org"}, |
| 65 | + follow_redirects=False |
| 66 | + ) |
| 67 | + |
| 68 | + assert response.status_code == 303 # Unauthorized |
0 commit comments