Created
January 30, 2019 09:39
-
-
Save Lunik/f1b70feb89f9b40d9ff8fa26d484a5b2 to your computer and use it in GitHub Desktop.
Ansible unit test for gitlab_user
This file contains hidden or 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
from gitlab import Gitlab | |
from gitlab.v4.objects import * | |
from httmock import HTTMock # noqa | |
from httmock import response # noqa | |
from httmock import urlmatch # noqa | |
import requests | |
from ansible.module_utils.basic import AnsibleModule | |
from ansible.modules.source_control.gitlab_user import GitLabUser | |
try: | |
import unittest | |
except ImportError: | |
pass | |
class FakeAnsibleModule(object): | |
def __init__(self): | |
self.check_mode = False | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/users", method="get") | |
def resp_find_user(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('[{"id": 1, "username": "john_smith", "name": "John Smith", "state": "active",' | |
'"avatar_url": "http://localhost:3000/uploads/user/avatar/1/cd8.jpeg",' | |
'"web_url": "http://localhost:3000/john_smith"}, {"id": 2,' | |
'"username": "jack_smith", "name": "Jack Smith", "state": "blocked",' | |
'"avatar_url": "http://gravatar.com/../e32131cd8.jpeg",' | |
'"web_url": "http://localhost:3000/jack_smith"}]') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/users", method="post") | |
def resp_create_user(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "username": "john_smith", "name": "John Smith", "state": "active",' | |
'"avatar_url": "http://localhost:3000/uploads/user/avatar/1/cd8.jpeg",' | |
'"web_url": "http://localhost:3000/john_smith","created_at": "2012-05-23T08:00:58Z",' | |
'"bio": null, "location": null, "public_email": "[email protected]", "skype": "",' | |
'"linkedin": "", "twitter": "", "website_url": "", "organization": ""}') | |
content = content.encode("utf-8") | |
return response(201, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/users/1", method="get") | |
def resp_get_user(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "username": "john_smith", "name": "John Smith",' | |
'"state": "active",' | |
'"avatar_url": "http://localhost:3000/uploads/user/avatar/1/cd8.jpeg",' | |
'"web_url": "http://localhost:3000/john_smith",' | |
'"created_at": "2012-05-23T08:00:58Z", "bio": null, "location": null,' | |
'"public_email": "[email protected]", "skype": "", "linkedin": "",' | |
'"twitter": "", "website_url": "", "organization": "", "is_admin": false}') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/users/1/keys", method="get") | |
def resp_get_user_keys(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('[{"id": 1, "title": "Public key",' | |
'"key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596' | |
'k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQa' | |
'SeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=",' | |
'"created_at": "2014-08-01T14:47:39.080Z"},{"id": 3,' | |
'"title": "Another Public key",' | |
'"key": "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596' | |
'k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4soW6NUlfDzpvZK2H5E7eQaS' | |
'eP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0=",' | |
'"created_at": "2014-08-01T14:47:39.080Z"}]') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/users/1/keys", method="post") | |
def resp_create_user_keys(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "title": "Private key",' | |
'"key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcUdRuSuA5z' | |
'szUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+xawxKWmI7hJ5S0tOv6MJ+Ixy' | |
'Ta4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2jTiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH' | |
'2WOKBw6za0az6XoG75obUdFVdW3qcD0xc809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9' | |
'M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF",' | |
'"created_at": "2014-08-01T14:47:39.080Z"}') | |
content = content.encode("utf-8") | |
return response(201, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups", method="get") | |
def resp_find_group(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('[{"id": 1, "name": "Foobar Group", "path": "foo-bar",' | |
'"description": "An interesting group", "visibility": "public",' | |
'"lfs_enabled": true, "avatar_url": "http://localhost:3000/uploads/group/avatar/1/foo.jpg",' | |
'"web_url": "http://localhost:3000/groups/foo-bar", "request_access_enabled": false,' | |
'"full_name": "Foobar Group", "full_path": "foo-bar",' | |
'"file_template_project_id": 1, "parent_id": null}, {"id": 2, "name": "BarFoo Group", "path": "bar-foor",' | |
'"description": "An interesting group", "visibility": "public",' | |
'"lfs_enabled": true, "avatar_url": "http://localhost:3000/uploads/group/avatar/2/bar.jpg",' | |
'"web_url": "http://localhost:3000/groups/bar-foo", "request_access_enabled": false,' | |
'"full_name": "BarFoo Group", "full_path": "bar-foo",' | |
'"file_template_project_id": 1, "parent_id": null}]') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1", method="get") | |
def resp_get_group(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "name": "Foobar Group", "path": "foo-bar",' | |
'"description": "An interesting group", "visibility": "public",' | |
'"lfs_enabled": true, "avatar_url": "http://localhost:3000/uploads/group/avatar/1/foo.jpg",' | |
'"web_url": "http://localhost:3000/groups/foo-bar", "request_access_enabled": false,' | |
'"full_name": "Foobar Group", "full_path": "foo-bar",' | |
'"file_template_project_id": 1, "parent_id": null}') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/members/1", method="get") | |
def resp_get_member(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "username": "raymond_smith", "name": "Raymond Smith", "state": "active",' | |
'"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",' | |
'"web_url": "http://192.168.1.8:3000/root", "expires_at": "2012-10-22T14:13:35Z", "access_level": 30}') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/members", method="get") | |
def resp_find_member(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('[{"id": 1, "username": "raymond_smith", "name": "Raymond Smith", "state": "active",' | |
'"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",' | |
'"web_url": "http://192.168.1.8:3000/root", "expires_at": "2012-10-22T14:13:35Z", "access_level": 30},{' | |
'"id": 2, "username": "john_doe", "name": "John Doe","state": "active",' | |
'"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",' | |
'"web_url": "http://192.168.1.8:3000/root","expires_at": "2012-10-22T14:13:35Z",' | |
'"access_level": 30}]') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/members", method="post") | |
def resp_add_member(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "username": "raymond_smith", "name": "Raymond Smith",' | |
'"state": "active",' | |
'"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",' | |
'"web_url": "http://192.168.1.8:3000/root", "expires_at": "2012-10-22T14:13:35Z",' | |
'"access_level": 30}') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
@urlmatch(scheme="http", netloc="localhost", path="/api/v4/groups/1/members/1", method="put") | |
def resp_update_member(url, request): | |
headers = {'content-type': 'application/json'} | |
content = ('{"id": 1, "username": "raymond_smith", "name": "Raymond Smith",' | |
'"state": "active",' | |
'"avatar_url": "https://www.gravatar.com/avatar/c2525a7f58ae3776070e44c106c48e15?s=80&d=identicon",' | |
'"web_url": "http://192.168.1.8:3000/root", "expires_at": "2012-10-22T14:13:35Z",' | |
'"access_level": 10}') | |
content = content.encode("utf-8") | |
return response(200, content, headers, None, 5, request) | |
class TestGitlabUser(unittest.TestCase): | |
def setUp(self): | |
self.gitlab_instance = Gitlab("http://localhost", private_token="private_token", api_version=4) | |
self.moduleUtil = GitLabUser(module=FakeAnsibleModule(), gitlab_instance=self.gitlab_instance) | |
def test_findUser(self): | |
with HTTMock(resp_find_user): | |
user = self.moduleUtil.findUser("john_smith") | |
self.assertEqual(type(user), User) | |
self.assertEqual(user.name, "John Smith") | |
self.assertEqual(user.id, 1) | |
def test_create_user(self): | |
with HTTMock(resp_create_user): | |
user = self.moduleUtil.createUser({'email': '[email protected]', 'password': 's3cur3s3cr3T', | |
'username': 'john_smith', 'name': 'John Smith'}) | |
self.assertEqual(type(user), User) | |
self.assertEqual(user.name, "John Smith") | |
self.assertEqual(user.id, 1) | |
def test_update_user(self): | |
with HTTMock(resp_get_user): | |
user = self.gitlab_instance.users.get(1) | |
changed, newUser = self.moduleUtil.updateUser(user, {'name': "Jack Smith", "is_admin": "true"}) | |
self.assertEqual(changed, True) | |
self.assertEqual(newUser.name, "Jack Smith") | |
self.assertEqual(newUser.is_admin, "true") | |
changed, newUser = self.moduleUtil.updateUser(user, {'name': "Jack Smith"}) | |
self.assertEqual(changed, False) | |
def test_sshkey_exist(self): | |
with HTTMock(resp_get_user): | |
with HTTMock(resp_get_user_keys): | |
user = self.gitlab_instance.users.get(1) | |
exist = self.moduleUtil.sshKeyExists(user, "Public key") | |
self.assertEqual(exist, True) | |
notExist = self.moduleUtil.sshKeyExists(user, "Private key") | |
self.assertEqual(notExist, False) | |
def test_create_sshkey(self): | |
with HTTMock(resp_get_user): | |
with HTTMock(resp_create_user_keys): | |
with HTTMock(resp_get_user_keys): | |
user = self.gitlab_instance.users.get(1) | |
rvalue = self.moduleUtil.addSshKeyToUser(user, { | |
'name': "Public key", | |
'file': "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJe" | |
"jgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9wa++Oi7Qkr8prgHc4" | |
"soW6NUlfDzpvZK2H5E7eQaSeP3SAwGmQKUFHCddNaP0L+hM7zhFNzjFvpaMgJw0="}) | |
self.assertEqual(rvalue, False) | |
rvalue = self.moduleUtil.addSshKeyToUser(user, { | |
'name': "Private key", | |
'file': "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDA1YotVDm2mAyk2tPt4E7AHm01sS6JZmcU" | |
"dRuSuA5zszUJzYPPUSRAX3BCgTqLqYx//UuVncK7YqLVSbbwjKR2Ez5lISgCnVfLVEXzwhv+" | |
"xawxKWmI7hJ5S0tOv6MJ+IxyTa4xcKwJTwB86z22n9fVOQeJTR2dSOH1WJrf0PvRk+KVNY2j" | |
"TiGHTi9AIjLnyD/jWRpOgtdfkLRc8EzAWrWlgNmH2WOKBw6za0az6XoG75obUdFVdW3qcD0x" | |
"c809OHLi7FDf+E7U4wiZJCFuUizMeXyuK/SkaE1aee4Qp5R4dxTR4TP9M1XAYkf+kF0W9srZ+mhF069XD/zhUPJsvwEF"}) | |
self.assertEqual(rvalue, True) | |
def test_findMember(self): | |
with HTTMock(resp_get_group): | |
with HTTMock(resp_get_member): | |
group = self.gitlab_instance.groups.get(1) | |
user = self.moduleUtil.findMember(group, 1) | |
self.assertEqual(user.username, "raymond_smith") | |
def test_assignUserToGroup(self): | |
with HTTMock(resp_get_user): | |
with HTTMock(resp_get_group): | |
with HTTMock(resp_get_member): | |
with HTTMock(resp_add_member): | |
with HTTMock(resp_update_member): | |
group = self.gitlab_instance.groups.get(1) | |
user = self.gitlab_instance.users.get(1) | |
rvalue = self.moduleUtil.assignUserToGroup(user, group.id, "developer") | |
self.assertEqual(rvalue, False) | |
rvalue = self.moduleUtil.assignUserToGroup(user, group.id, "guest") | |
self.assertEqual(rvalue, True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment