class TestApiRocketChat(unittest.TestCase):
    """ Unit tests for ApiRocketChat Xblock"""
    def setUp(self):
        """Set up general variables"""

        user = "******"
        password = "******"
        server_url = "http://test_server"

        with patch('rocketc.api_rocket_chat.RocketChat.login'):
            self.api = ApiRocketChat(user, password, server_url)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.groups_invite')
    def test_add_user_to_group(self, mock_request):
        """Test for the add group method"""
        success = {'success': True}

        user_id = "test_user_id"
        room_id = "test_room_id"

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        response = self.api.add_user_to_group(user_id, room_id)
        mock_request.assert_called_with(room_id, user_id)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat.users_update')
    def test_change_user_role(self, mock_request):
        """Test for chage role method"""
        success = {'success': True}

        user_id = "test_user_id"
        role = "test_role"

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        user_role = self.api.change_user_role(user_id, role)
        mock_request.assert_called_with(user_id, data={"roles": [role]})
        self.assertEqual(role, user_role)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.groups_create')
    def test_create_group(self, mock_request):
        """Test for the create group method"""
        success = {'success': True}

        name = "test_name"
        username = ["test_user_name"]

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        response = self.api.create_group(name, username)
        self.assertEquals(response, success)
        mock_request.assert_called_with(name, members=username)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.users_create_token')
    def test_create_token(self, mock_request):
        """Test for the create token method"""
        success = {'success': True}
        username = "******"

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        response = self.api.create_token(username)

        mock_request.assert_called_with(username=username)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat.users_create')
    def test_create_user(self, mock_request):
        """Test for the create user method"""
        success = {'success': True}

        email = "test_email"
        username = "******"
        name = "test_name"
        salt = "HarryPotter_and_thePrisoner_of _Azkaban"

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        password = "******".format(name, salt)
        password = hashlib.sha1(password).hexdigest()
        response = self.api.create_user(name, email, username)
        mock_request.assert_called_with(email, name, password, username)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat.groups_list_all')
    def test_get_groups(self, mock_request):
        """Test the method to get a group list"""
        groups = {"groups": [{"name": "group1"}]}

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: groups)
        return_value = self.api.get_groups()

        mock_request.assert_called_with(**{})
        self.assertIn("group1", return_value)

        mock_request.return_value = MagicMock(status_code=200)

        return_value = self.api.get_groups()

        self.assertFalse(return_value)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.channels_info')
    @patch('rocketc.api_rocket_chat.ApiRocketChat.channels_set_type')
    def test_convert_to_private_channel(self, mock_channels_set_type,
                                        mock_channels_info):
        """Test for private channel method"""
        room_name = "test_room_name"

        mock_channels_info.return_value = MagicMock(
            status_code=200,
            json=lambda: {"channel": {
                "t": "c",
                "_id": "1234"
            }})

        self.api.convert_to_private_channel(room_name)
        mock_channels_set_type.assert_called_with("1234", "p")

    @patch('rocketc.api_rocket_chat.ApiRocketChat.groups_info')
    def test_search_rocket_chat_group(self, mock_request):
        """Test for the search group method"""
        success = {'success': True}
        room_name = "test_room_name"
        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        response = self.api.search_rocket_chat_group(room_name)

        mock_request.assert_called_with(room_name=room_name)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat.users_info')
    def test_search_rocket_chat_user(self, mock_request):
        """Test for the search user method"""
        success = {'success': True}
        username = "******"
        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        response = self.api.search_rocket_chat_user(username)

        mock_request.assert_called_with(username=username)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat.users_set_avatar')
    def test_set_avatar(self, mock_request):
        """Test the method for set the avatar in RocketChat"""
        username = "******"
        url = "test_url"
        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: {'success': True})
        self.api.set_avatar(username, url)
        mock_request.assert_called_with(url, username=username, avatarUrl=url)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.groups_set_description')
    def test_set_group_description(self, mock_request):
        """
        This method tests the method set_description
        """
        description = None
        group_id = "test_id"

        return_none = self.api.set_group_description(group_id, description)

        self.assertIsNone(return_none)

        description = "test_description"
        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: {'success': True})
        self.api.set_group_description(group_id, description)
        mock_request.assert_called_with(group_id, description)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.groups_set_topic')
    def test_set_group_topic(self, mock_request):
        """
        This method test the method set_topic
        """
        topic = None
        group_id = "test_id"

        return_none = self.api.set_group_topic(group_id, topic)

        self.assertIsNone(return_none)

        topic = "test_topic"
        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: {'success': True})
        self.api.set_group_topic(group_id, topic)
        mock_request.assert_called_with(group_id, topic)

    @patch('rocketc.api_rocket_chat.ApiRocketChat.users_update')
    def test_update_user(self, mock_request):
        """Test the method to update the profile user"""
        success = {'success': True}

        user_id = "test_user_id"
        email = "test_email"

        mock_request.return_value = MagicMock(status_code=200,
                                              json=lambda: success)

        new_email = self.api.update_user(user_id, email)

        self.assertEqual(new_email, email)
        mock_request.assert_called_with(user_id, data={"email": email})
class TestApiRocketChat(unittest.TestCase):
    """ Unit tests for ApiRocketChat Xblock"""
    def setUp(self):
        """Set up general variables"""

        user = "******"
        password = "******"
        server_url = "http://test_server"
        data = {"authToken": "test_token", "userId": "test_id"}

        with patch('rocketc.api_rocket_chat.requests.post') as mock_post:
            mock_post.return_value.json.return_value = data
            self.api = ApiRocketChat(user, password, server_url)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_add_user_to_group(self, mock_request):
        """Test for the add group method"""
        method = "post"
        success = {'success': True}

        user_id = "test_user_id"
        room_id = "test_room_id"

        mock_request.return_value = success
        url_path = "groups.invite"

        data = {"roomId": room_id, "userId": user_id}

        response = self.api.add_user_to_group(user_id, room_id)
        mock_request.assert_called_with(method, url_path, data)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_change_user_role(self, mock_request):
        """Test for chage role method"""
        method = "post"
        success = {'success': True}

        user_id = "test_user_id"
        role = "test_role"

        mock_request.return_value = success
        url_path = "users.update"

        data = {"userId": user_id, "data": {"roles": [role]}}

        self.api.change_user_role(user_id, role)
        mock_request.assert_called_with(method, url_path, data)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_create_group(self, mock_request):
        """Test for the create group method"""
        method = "post"
        success = {'success': True}

        name = "test_name"
        username = ["test_user_name"]

        mock_request.return_value = success
        url_path = "groups.create"

        data = {'name': name, "members": username}

        response = self.api.create_group(name, username)
        self.assertEquals(response, success)
        mock_request.assert_called_with(method, url_path, data)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_create_token(self, mock_request):
        """Test for the create token method"""
        method = "post"
        success = {'success': True}

        username = "******"

        mock_request.return_value = success
        url_path = "users.createToken"

        data = {'username': username}

        response = self.api.create_token(username)

        mock_request.assert_called_with(method, url_path, data)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_create_user(self, mock_request):
        """Test for the create user method"""
        method = "post"
        success = {'success': True}

        email = "test_email"
        username = "******"
        name = "test_name"
        salt = "HarryPotter_and_thePrisoner_of _Azkaban"

        mock_request.return_value = success
        url_path = "users.create"

        password = "******".format(name, salt)
        password = hashlib.sha1(password).hexdigest()
        data = {
            "name": name,
            "email": email,
            "password": password,
            "username": username
        }

        response = self.api.create_user(name, email, username)

        mock_request.assert_called_with(method, url_path, data)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_get_groups(self, mock_request):
        """Test the method to get a group list"""
        method = "get"
        url_path = "groups.list"

        groups = {"groups": [{"name": "group1"}]}
        mock_request.return_value = groups

        return_value = self.api.get_groups()

        mock_request.assert_called_with(method, url_path)
        self.assertIn("group1", return_value)

        mock_request.return_value = {}

        return_value = self.api.get_groups()

        self.assertFalse(return_value)

    @patch('rocketc.api_rocket_chat.requests.post')
    def test_login(self, mock_request):
        """"""
        user = "******"
        password = "******"
        url = "/".join([self.api.server_url, self.api.API_PATH, "login"])
        data = {"user": user, "password": password}

        mock_request.return_value = MagicMock(status_code=200)
        mock_request.return_value.json.return_value = {
            "data": {
                "authToken": "test_token",
                "userId": "test_id"
            }
        }

        self.api._login(user, password)

        mock_request.assert_called_with(url=url,
                                        json=data,
                                        headers=self.api.headers)
        self.assertEqual(self.api.headers["X-Auth-Token"], "test_token")
        self.assertEqual(self.api.headers["X-User-Id"], "test_id")

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_convert_to_private_channel(self, mock_request):
        """Test for private channel method"""
        method = "post"
        room_name = "test_room_name"
        url_path = "channels.setType"
        data = {"roomId": "1234", "type": "p"}

        mock_request.return_value = {"channel": {"t": "c", "_id": "1234"}}
        self.api.convert_to_private_channel(room_name)

        mock_request.assert_called_with(method, url_path, data)

    def test_request_rocket_chat(self):
        """Test for the request rocket chat method """
        users = [{
            "user": {
                "_id": "BsNr28znDkG8aeo7W",
                "createdAt": "2016-09-13T14:57:56.037Z",
            },
            "success": "true",
        }]

        info = [{"success": "true", "info": {"version": "0.47.0-develop"}}]

        with patch('rocketc.api_rocket_chat.requests.post') as mock_post:
            mock_post.return_value.json.return_value = users
            data_post = self.api._request_rocket_chat("post", "users.create")

        with patch('rocketc.api_rocket_chat.requests.get') as mock_get:
            mock_get.return_value.json.return_value = info
            data_get = self.api._request_rocket_chat("get", "info")

        self.assertEqual(data_post, users)
        self.assertEqual(data_get, info)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_search_rocket_chat_group(self, mock_request):
        """Test for the search group method"""
        method = "get"
        success = {'success': True}
        room_name = "test_room_name"
        mock_request.return_value = success
        url_path = "groups.info"
        payload = {"roomName": room_name}

        response = self.api.search_rocket_chat_group(room_name)

        mock_request.assert_called_with(method, url_path, payload=payload)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_search_rocket_chat_user(self, mock_request):
        """Test for the search user method"""
        method = "get"
        success = {'success': True}
        username = "******"
        mock_request.return_value = success
        url_path = "users.info"
        payload = {"username": username}

        response = self.api.search_rocket_chat_user(username)

        mock_request.assert_called_with(method, url_path, payload=payload)
        self.assertTrue(response['success'])

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_set_avatar(self, mock_request):
        """Test the method for set the avatar in RocketChat"""
        method = "post"
        username = "******"
        url = "test_url"

        url_path = "users.setAvatar"

        data = {"username": username, "avatarUrl": url}
        self.api.set_avatar(username, url)
        mock_request.assert_called_with(method, url_path, data)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_set_group_description(self, mock_request):
        """
        This method tests the method set_description
        """
        description = None
        group_id = "test_id"

        return_none = self.api.set_group_description(group_id, description)

        self.assertIsNone(return_none)

        description = "test_description"

        url_path = "groups.setDescription"
        data = {"roomId": group_id, "description": description}
        method = "post"

        self.api.set_group_description(group_id, description)
        mock_request.assert_called_with(method, url_path, data)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_set_group_topic(self, mock_request):
        """
        This method test the method set_topic
        """
        topic = None
        group_id = "test_id"

        return_none = self.api.set_group_topic(group_id, topic)

        self.assertIsNone(return_none)

        topic = "test_topic"

        url_path = "groups.setTopic"
        data = {"roomId": group_id, "topic": topic}
        method = "post"

        self.api.set_group_topic(group_id, topic)
        mock_request.assert_called_with(method, url_path, data)

    @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat')
    def test_update_user(self, mock_request):
        """Test the method to update the profile user"""
        method = "post"
        success = {'success': True}

        user_id = "test_user_id"
        email = "test_email"

        mock_request.return_value = success
        url_path = "users.update"

        data = {"userId": user_id, "data": {"email": email}}

        new_email = self.api.update_user(user_id, email)

        self.assertEqual(new_email, email)
        mock_request.assert_called_with(method, url_path, data)