-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
379 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
import os | ||
from unittest.mock import Mock | ||
from unittest.mock import patch | ||
|
||
from landscape.client.manager.plugin import FAILED | ||
from landscape.client.manager.plugin import SUCCEEDED | ||
|
@@ -9,6 +10,7 @@ | |
from landscape.client.tests.helpers import LandscapeTest | ||
from landscape.client.tests.helpers import ManagerHelper | ||
from landscape.client.user.provider import UserManagementError | ||
from landscape.client.user.tests.helpers import FakeSnapdUserManagement | ||
from landscape.client.user.tests.helpers import FakeUserManagement | ||
from landscape.client.user.tests.helpers import FakeUserProvider | ||
from landscape.lib.persist import Persist | ||
|
@@ -36,14 +38,18 @@ def tearDown(self): | |
for plugin in self.plugins: | ||
plugin.stop() | ||
|
||
def setup_environment(self, users, groups, shadow_file): | ||
def setup_environment(self, users, groups, shadow_file, is_core=False): | ||
provider = FakeUserProvider( | ||
users=users, | ||
groups=groups, | ||
shadow_file=shadow_file, | ||
) | ||
user_monitor = UserMonitor(provider=provider) | ||
management = FakeUserManagement(provider=provider) | ||
|
||
if is_core: | ||
management = FakeSnapdUserManagement(provider=provider) | ||
else: | ||
management = FakeUserManagement(provider=provider) | ||
user_manager = UserManager( | ||
management=management, | ||
shadow_file=shadow_file, | ||
|
@@ -378,6 +384,64 @@ def handle_callback(result): | |
result.addCallback(handle_callback) | ||
return result | ||
|
||
@patch("landscape.client.manager.usermanager.IS_CORE", "1") | ||
def test_add_user_event_on_core(self): | ||
""" | ||
When an C{add-user} event is received the user should be | ||
added. Two messages should be generated: a C{users} message | ||
with details about the change and an C{operation-result} with | ||
details of the outcome of the operation. | ||
""" | ||
|
||
def handle_callback(result): | ||
messages = self.broker_service.message_store.get_pending_messages() | ||
self.assertMessages( | ||
messages, | ||
[ | ||
{ | ||
"type": "operation-result", | ||
"status": SUCCEEDED, | ||
"operation-id": 123, | ||
"timestamp": 0, | ||
"result-text": "add_user succeeded", | ||
}, | ||
{ | ||
"timestamp": 0, | ||
"type": "users", | ||
"operation-id": 123, | ||
"create-users": [ | ||
{ | ||
"home-phone": None, | ||
"username": "john-doe", | ||
"uid": 1000, | ||
"enabled": True, | ||
"location": None, | ||
"work-phone": None, | ||
"name": "[email protected]", | ||
"primary-gid": 1000, | ||
}, | ||
], | ||
}, | ||
], | ||
) | ||
|
||
shadow_file = self.makeFile("""st3v3nmw:*:19758:0:99999:7:::""") | ||
self.setup_environment([], [], shadow_file, is_core=True) | ||
|
||
result = self.manager.dispatch_message( | ||
{ | ||
"type": "add-user", | ||
"username": "john-doe", | ||
"email": "[email protected]", | ||
"sudoer": False, | ||
"force-managed": True, | ||
"operation-id": 123, | ||
}, | ||
) | ||
|
||
result.addCallback(handle_callback) | ||
return result | ||
|
||
def test_edit_user_event(self): | ||
""" | ||
When a C{edit-user} message is received the user should be | ||
|
@@ -834,6 +898,62 @@ def handle_callback(result): | |
result.addCallback(handle_callback) | ||
return result | ||
|
||
@patch("landscape.client.manager.usermanager.IS_CORE", "1") | ||
def test_remove_user_event_on_core(self): | ||
""" | ||
When a C{remove-user} event is received, the user should be removed. | ||
Two messages should be generated: a C{users} message with details | ||
about the change and an C{operation-result} with details of the | ||
outcome of the operation. | ||
""" | ||
|
||
def handle_callback(result): | ||
messages = self.broker_service.message_store.get_pending_messages() | ||
self.assertEqual(len(messages), 3) | ||
# Ignore the message created by plugin.run. | ||
self.assertMessages( | ||
[messages[2], messages[1]], | ||
[ | ||
{ | ||
"timestamp": 0, | ||
"delete-users": ["john-doe"], | ||
"type": "users", | ||
"operation-id": 39, | ||
}, | ||
{ | ||
"type": "operation-result", | ||
"status": SUCCEEDED, | ||
"operation-id": 39, | ||
"timestamp": 0, | ||
"result-text": "remove_user succeeded", | ||
}, | ||
], | ||
) | ||
|
||
users = [ | ||
( | ||
"john-doe", | ||
"x", | ||
1000, | ||
1000, | ||
"[email protected],BtrGAhK,,", | ||
"/home/user", | ||
"/bin/zsh", | ||
), | ||
] | ||
shadow_file = self.makeFile("""st3v3nmw:*:19758:0:99999:7:::""") | ||
self.setup_environment(users, [], shadow_file, is_core=True) | ||
result = self.manager.dispatch_message( | ||
{ | ||
"username": "john-doe", | ||
"delete-home": True, | ||
"type": "remove-user", | ||
"operation-id": 39, | ||
}, | ||
) | ||
result.addCallback(handle_callback) | ||
return result | ||
|
||
def test_lock_user_event(self): | ||
""" | ||
When a C{lock-user} event is received the user should be | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import os | ||
from unittest.mock import ANY | ||
from unittest.mock import Mock | ||
from unittest.mock import patch | ||
|
||
from twisted.internet.defer import fail | ||
|
||
|
@@ -208,6 +209,50 @@ def test_new_message_after_resynchronize_event(self): | |
], | ||
) | ||
|
||
@patch("landscape.client.manager.usermanager.IS_CORE", "1") | ||
def test_new_message_after_resynchronize_event_on_core(self): | ||
""" | ||
When a 'resynchronize' reactor event is fired, a new session is | ||
created and the UserMonitor creates a new message. | ||
""" | ||
self.provider.users = [ | ||
( | ||
"john-doe", | ||
"x", | ||
1000, | ||
1000, | ||
"[email protected]", | ||
"/home/user", | ||
"/bin/zsh", | ||
), | ||
] | ||
self.broker_service.message_store.set_accepted_types(["users"]) | ||
plugin = UserMonitor(self.provider) | ||
self.monitor.add(plugin) | ||
plugin.client.broker.message_store.drop_session_ids() | ||
deferred = self.reactor.fire("resynchronize")[0] | ||
self.successResultOf(deferred) | ||
self.assertMessages( | ||
self.broker_service.message_store.get_pending_messages(), | ||
[ | ||
{ | ||
"create-users": [ | ||
{ | ||
"enabled": True, | ||
"home-phone": None, | ||
"location": None, | ||
"name": "[email protected]", | ||
"primary-gid": 1000, | ||
"uid": 1000, | ||
"username": "john-doe", | ||
"work-phone": None, | ||
}, | ||
], | ||
"type": "users", | ||
}, | ||
], | ||
) | ||
|
||
def test_wb_resynchronize_event_with_global_scope(self): | ||
""" | ||
When a C{resynchronize} event, with global scope, occurs we act exactly | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.