|
| 1 | +"""Tests for add/remove tool management commands |
| 2 | +
|
| 3 | +@author: GitStart |
| 4 | +
|
| 5 | +@license: |
| 6 | +This Source Code Form is subject to the terms of the Mozilla Public |
| 7 | +License, v. 2.0. If a copy of the MPL was not distributed with this |
| 8 | +file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 9 | +""" |
| 10 | +import pytest |
| 11 | +from django.core.management import CommandError, call_command |
| 12 | +from django.contrib.auth.models import User |
| 13 | +from rest_framework.authtoken.models import Token |
| 14 | +from crashmanager.models import Tool, User as CrashManagerUser |
| 15 | + |
| 16 | +pytestmark = pytest.mark.django_db() |
| 17 | + |
| 18 | +def test_add_tool_to_token_new_tool(capsys): |
| 19 | + """Test adding new tool to user's filter""" |
| 20 | + user = User.objects.create_user("testuser") |
| 21 | + token = Token.objects.create(user=user) |
| 22 | + |
| 23 | + call_command("add_tool_to_token", token.key, "newtool") |
| 24 | + |
| 25 | + # Check command output |
| 26 | + out, _ = capsys.readouterr() |
| 27 | + assert "Tool 'newtool' was created." in out |
| 28 | + assert "added to user" in out |
| 29 | + |
| 30 | + # Verify database state |
| 31 | + cm_user = CrashManagerUser.objects.get(user=user) |
| 32 | + assert cm_user.restricted is True |
| 33 | + assert cm_user.defaultToolsFilter.filter(name="newtool").exists() |
| 34 | + |
| 35 | +def test_add_tool_to_token_existing_tool(capsys): |
| 36 | + """Test adding existing tool doesn't recreate it""" |
| 37 | + # Create tool first |
| 38 | + Tool.objects.create(name="existingtool") |
| 39 | + |
| 40 | + user = User.objects.create_user("testuser") |
| 41 | + token = Token.objects.create(user=user) |
| 42 | + |
| 43 | + call_command("add_tool_to_token", token.key, "existingtool") |
| 44 | + |
| 45 | + # Check command output |
| 46 | + out, _ = capsys.readouterr() |
| 47 | + assert "Tool 'existingtool' was created." not in out |
| 48 | + assert "added to user" in out |
| 49 | + |
| 50 | +def test_add_tool_to_token_restricts_user(capsys): |
| 51 | + """Test unrestricted user becomes restricted when adding tool""" |
| 52 | + user = User.objects.create_user("testuser") |
| 53 | + cm_user = CrashManagerUser.get_or_create_restricted(user)[0] |
| 54 | + cm_user.restricted = False |
| 55 | + cm_user.save() |
| 56 | + |
| 57 | + token = Token.objects.create(user=user) |
| 58 | + |
| 59 | + call_command("add_tool_to_token", token.key, "newtool") |
| 60 | + |
| 61 | + # Check warning message |
| 62 | + out, _ = capsys.readouterr() |
| 63 | + assert "has been restricted for security" in out |
| 64 | + |
| 65 | + # Verify restriction |
| 66 | + cm_user.refresh_from_db() |
| 67 | + assert cm_user.restricted is True |
| 68 | + |
| 69 | +def test_remove_tool_from_token_exists(capsys): |
| 70 | + """Test removing existing tool from filter""" |
| 71 | + user = User.objects.create_user("testuser") |
| 72 | + cm_user = CrashManagerUser.get_or_create_restricted(user)[0] |
| 73 | + tool = Tool.objects.create(name="oldtool") |
| 74 | + cm_user.defaultToolsFilter.add(tool) |
| 75 | + |
| 76 | + token = Token.objects.create(user=user) |
| 77 | + |
| 78 | + call_command("remove_tool_from_token", token.key, "oldtool") |
| 79 | + |
| 80 | + # Check output |
| 81 | + out, _ = capsys.readouterr() |
| 82 | + assert "removed from user" in out |
| 83 | + assert not cm_user.defaultToolsFilter.filter(name="oldtool").exists() |
| 84 | + |
| 85 | +def test_remove_tool_from_token_last_tool(capsys): |
| 86 | + """Test warning when removing last tool""" |
| 87 | + user = User.objects.create_user("testuser") |
| 88 | + cm_user = CrashManagerUser.get_or_create_restricted(user)[0] |
| 89 | + tool = Tool.objects.create(name="lasttool") |
| 90 | + cm_user.defaultToolsFilter.add(tool) |
| 91 | + |
| 92 | + token = Token.objects.create(user=user) |
| 93 | + |
| 94 | + call_command("remove_tool_from_token", token.key, "lasttool") |
| 95 | + |
| 96 | + # Check warning |
| 97 | + out, _ = capsys.readouterr() |
| 98 | + assert "has no tools assigned" in out |
| 99 | + |
| 100 | + # Refresh from DB to get updated restriction status |
| 101 | + cm_user.refresh_from_db() |
| 102 | + assert cm_user.restricted is True |
| 103 | + |
| 104 | +def test_remove_tool_from_token_nonexistent(capsys): |
| 105 | + """Test removing non-existent tool shows error""" |
| 106 | + user = User.objects.create_user("testuser") |
| 107 | + token = Token.objects.create(user=user) |
| 108 | + |
| 109 | + # Should return normally with error message |
| 110 | + call_command("remove_tool_from_token", token.key, "notexist") |
| 111 | + |
| 112 | + # Verify error message |
| 113 | + out, _ = capsys.readouterr() |
| 114 | + assert "Error: Tool 'notexist' is not present in the database" in out |
| 115 | + |
| 116 | + # Verify no changes to tools |
| 117 | + cm_user = CrashManagerUser.objects.get(user=user) |
| 118 | + assert cm_user.defaultToolsFilter.count() == 0 |
| 119 | + |
| 120 | +def test_add_tool_to_token_invalid_token(capsys): |
| 121 | + """Test error handling for invalid token""" |
| 122 | + call_command("add_tool_to_token", "invalid", "tool") |
| 123 | + out, _ = capsys.readouterr() |
| 124 | + assert "No token found for invalid" in out |
| 125 | + |
| 126 | +def test_remove_tool_from_token_invalid_token(capsys): |
| 127 | + """Test error handling for invalid token""" |
| 128 | + call_command("remove_tool_from_token", "invalid", "tool") |
| 129 | + out, _ = capsys.readouterr() |
| 130 | + assert "No token found for invalid" in out |
0 commit comments