Skip to content

Commit febfc43

Browse files
committed
test coverage and linting
1 parent 3ee0c76 commit febfc43

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

ansible_base/activitystream/models/entry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def __str__(self):
6868
operation_text = self.get_operation_display()
6969
created_by_text = str(self.created_by) if self.created_by else "Unknown"
7070
return f'[{self.created}] Role assignment {operation_text.lower()} by {created_by_text}'
71-
71+
7272
# Standard format for other entry types
7373
return f'[{self.created}] {self.get_operation_display()} by {self.created_by}: {self.content_type} {self.object_id}'
7474

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
"""Test dummy models for optional django-ansible-base apps."""
2+
3+
import pytest
4+
5+
from ansible_base.rbac.models.dummy_models import DummyAuditableModel
6+
7+
8+
class TestDummyModel(DummyAuditableModel):
9+
"""Test model using DummyAuditableModel for testing."""
10+
11+
class Meta:
12+
app_label = 'test_app'
13+
14+
15+
@pytest.mark.django_db
16+
def test_dummy_auditable_model_interface():
17+
"""Test that DummyAuditableModel provides expected interface without crashing."""
18+
# Create test instance
19+
test_obj = TestDummyModel()
20+
21+
# Test activity_stream_entries property
22+
entries = test_obj.activity_stream_entries
23+
assert entries.count() == 0, "Dummy model should return empty count"
24+
assert entries.last() is None, "Dummy model should return None for last()"
25+
assert list(entries) == [], "Dummy model should return empty list"
26+
assert entries.all() == entries, "all() should return self"
27+
assert entries.order_by('id') == entries, "order_by() should return self"
28+
29+
# Test class attributes
30+
assert hasattr(test_obj, 'activity_stream_excluded_field_names')
31+
assert test_obj.activity_stream_excluded_field_names == []
32+
assert hasattr(test_obj, 'activity_stream_limit_field_names')
33+
assert test_obj.activity_stream_limit_field_names == []
34+
35+
# Test extra_related_fields method
36+
assert test_obj.extra_related_fields(None) == {}
37+
38+
39+
def test_dummy_model_prevents_import_crashes():
40+
"""Test that DummyAuditableModel can be imported without activitystream app."""
41+
# This test verifies the import works (which it does if we got here)
42+
# The real test is that importing dummy_models doesn't crash AWX/EDA
43+
from ansible_base.rbac.models.dummy_models import DummyAuditableModel
44+
45+
# Verify it's abstract
46+
assert DummyAuditableModel._meta.abstract is True
47+
48+
# Verify expected interface exists
49+
dummy = DummyAuditableModel()
50+
assert hasattr(dummy, 'activity_stream_entries')
51+
assert hasattr(dummy, 'extra_related_fields')
52+
assert hasattr(dummy, 'activity_stream_excluded_field_names')
53+
assert hasattr(dummy, 'activity_stream_limit_field_names')

test_app/tests/rbac/test_rbac_activity_stream.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ def test_role_user_assignment_activity_stream_lifecycle(system_user, admin_user,
5656
assert assignment.activity_stream_entries.count() == 1
5757
create_entry = assignment.activity_stream_entries.last()
5858
verify_activity_entry_fields(create_entry, 'create', admin_user, test_user.id, role_def.id, 'user')
59-
59+
6060
# Verify enhanced string representation
6161
entry_str = str(create_entry)
6262
assert "created" in entry_str.lower()
6363
assert str(admin_user) in entry_str
6464

65-
6665
# Delete assignment and verify DELETE entry
6766
assignment_id = assignment.id
6867
with impersonate(admin_user):
@@ -79,7 +78,7 @@ def test_role_user_assignment_activity_stream_lifecycle(system_user, admin_user,
7978
assert assignment_entries.count() == 2
8079
delete_entry = assignment_entries.last()
8180
verify_activity_entry_fields(delete_entry, 'delete', admin_user, test_user.id, role_def.id, 'user')
82-
81+
8382
# Verify enhanced string representation for delete
8483
delete_str = str(delete_entry)
8584
assert "deleted" in delete_str.lower()
@@ -128,3 +127,21 @@ def test_role_team_assignment_activity_stream(admin_user, team, organization):
128127
assert assignment_entries.count() == 2
129128
delete_entry = assignment_entries.last()
130129
verify_activity_entry_fields(delete_entry, 'delete', admin_user, team.id, role_def.id, 'team')
130+
131+
132+
@pytest.mark.skipif(not apps.is_installed('ansible_base.activitystream'), reason="Activity stream tests only run when activitystream app is installed")
133+
@pytest.mark.django_db
134+
def test_non_role_assignment_entry_str_unchanged(admin_user):
135+
"""Test that non-role assignment entries still use standard __str__() format."""
136+
from test_app.models import Organization
137+
138+
# Create a regular organization (not a role assignment)
139+
org = Organization.objects.create(name='TestOrg_Standard')
140+
141+
# Get the activity entry for the organization creation
142+
org_entry = org.activity_stream_entries.last()
143+
144+
# Verify it uses the standard format (not enhanced role assignment format)
145+
entry_str = str(org_entry)
146+
assert 'test_app | organization' in entry_str, "Non-role entries should use standard format"
147+
assert 'Role assignment' not in entry_str, "Non-role entries should not use role assignment format"

0 commit comments

Comments
 (0)