From c3024fdeb5ed4b2383b1cec8b3f95e427de54f34 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Thu, 26 Apr 2018 16:01:24 -0500 Subject: [PATCH 1/8] reorder methods --- rocketc/api_rocket_chat.py | 196 +++++++++++++++++ rocketc/rocketc.py | 421 ++++++++++++------------------------- 2 files changed, 325 insertions(+), 292 deletions(-) create mode 100644 rocketc/api_rocket_chat.py diff --git a/rocketc/api_rocket_chat.py b/rocketc/api_rocket_chat.py new file mode 100644 index 00000000..065a6374 --- /dev/null +++ b/rocketc/api_rocket_chat.py @@ -0,0 +1,196 @@ +""" +This file contains the class with the necessary methos to use rocketchat +""" +import hashlib +import requests +import logging + +LOG = logging.getLogger(__name__) + + +class ApiRocketChat(object): + + headers = {"Content-type": "application/json"} + API_PATH = 'api/v1' + salt = "HarryPotter_and_thePrisoner_of _Azkaban" + + def __init__(self, user, password, server_url="http://127.0.0.1:3000"): + """Creates a RocketChat object and does login on the specified server""" + self.server_url = server_url + self._login(user, password) + + def _login(self, user, password): + """""" + url = "/".join([self.server_url, self.API_PATH, "login"]) + data = {"user": user, "password": password} + response = requests.post(url=url, json=data, headers=self.headers) + if response.status_code == 200: + response = response.json()["data"] + self.headers["X-Auth-Token"] = response["authToken"] + self.headers["X-User-Id"] = response["userId"] + + LOG.info("Auth_token: %s, User_id: %s ", self.headers[ + "X-Auth-Token"], self.headers["X-User-Id"]) + + def _request_rocket_chat(self, method, url_path, data=None): + """ + This method generates a call to the RocketChat API and returns a json with the response + """ + url = "/".join([self.server_url, self.API_PATH, url_path]) + if method == "post": + response = requests.post(url=url, json=data, headers=self.headers) + LOG.info("Request rocketChat status code = %s", response.status_code) + else: + response = requests.get(url=url, headers=self.headers) + LOG.info("Request rocketChat status code = %s", response.status_code) + return response.json() + + def add_to_group(self, user_id, room_id): + """ + This method add any user to any group + """ + url_path = "groups.invite" + data = {"roomId": room_id, "userId": user_id} + response = self._request_rocket_chat("post", url_path, data) + LOG.info("Method Add to Group: %s with this data: %s", response, data) + return response + + def change_role(self, user_id, role): + """ + This method allows to change the user's role + """ + url_path = "users.update" + data = {"userId": user_id, "data": {"roles": [role]}} + response = self._request_rocket_chat( + "post", url_path, data) + if response["success"]: + return role + + def create_group(self, name, username=""): + """ + This method creates a group with a specific name. + """ + url_path = "groups.create" + data = {"name": name, "members": [username]} + response = self._request_rocket_chat("post", url_path, data) + LOG.info("Method Create Group: %s with this data: %s", response, data) + return response + + def create_token(self, username): + """ + This method generates a token that allows to login + """ + url_path = "users.createToken" + data = {'username': username} + response = self._request_rocket_chat("post", url_path, data) + LOG.info("Method Create Token: %s with this data: %s", response, data) + return response + + def create_user(self, name, email, username): + """ + This method creates a user with a specific name, username, email and password. + The password is a result from a SHA1 function with the name and salt. + """ + url_path = "users.create" + password = "{}{}".format(name, self.salt) + password = hashlib.sha1(password).hexdigest() + data = {"name": name, "email": email, + "password": password, "username": username} + response = self._request_rocket_chat("post", url_path, data) + LOG.info("Method Create User: %s with this data: %s", response, data) + return response + + def get_groups(self): + """ + This method lists the existing groups + """ + url_path = "groups.list" + method = "get" + response = self._request_rocket_chat(method, url_path) + list_groups = [] + + if "groups" in response: + for group in response["groups"]: + list_groups.append(group["name"]) + + return list_groups + + def private_channel(self, room_name): + """ + This method changes channels from public to private + the channel's type is define as t, when t = c is a public channel + and when t = p is a private channel + """ + url_search = "{}?{}={}".format("channels.info", "roomName", room_name) + channel = self._request_rocket_chat("get", url_search) + + if "channel" in channel and channel["channel"]["t"] == "c": + channel_id = channel["channel"]["_id"] + url_path = "channels.setType" + data = {"roomId": channel_id, "type": "p"} + self._request_rocket_chat("post", url_path, data) + + def search_rocket_chat_group(self, room_name): + """ + This method gets a group with a specific name and returns a json with group's info + """ + url_path = "{}?{}={}".format("groups.info", "roomName", room_name) + return self._request_rocket_chat("get", url_path) + + def search_rocket_chat_user(self, username): + """ + This method allows to get a user from RocketChat data base + """ + url_path = "{}?{}={}".format("users.info", "username", username) + + return self._request_rocket_chat("get", url_path) + + def set_avatar(self, username, image_url): + """ + This method allows to set the profile photo/avatar + """ + url_path = "users.setAvatar" + data = {"username": username, "avatarUrl": image_url} + response = self._request_rocket_chat("post", url_path, data) + LOG.info("Method set avatar: %s with this data: %s", response, data) + + def set_description(self, group_id, description): + """ + This method allows to set a description's group + """ + if description == "" or description is None: + return + + url_path = "groups.setDescription" + data = {"roomId": group_id, "description": description} + method = "post" + + response = self._request_rocket_chat(method, url_path, data) + + LOG.info("Method Set Description %s with this data: %s", response, data) + + def set_topic(self, group_id, topic): + """ + This method allows to set a topic's group + """ + if topic == "" or topic is None: + return + + url_path = "groups.setTopic" + data = {"roomId": group_id, "topic": topic} + method = "post" + + response = self._request_rocket_chat(method, url_path, data) + + LOG.info("Method Set Topic: %s with this data: %s", response, data) + + def update_user(self, user_id, username, email): + """ + This method allows to update The user data + """ + url_path = "users.update" + data = {"userId": user_id, "data": {"email": email}} + response = self._request_rocket_chat("post", url_path, data) + LOG.info("Method Update User: %s with this data: %s", response, data) + if response["success"]: + return email diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index 4aed32dc..5478ed95 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -5,9 +5,9 @@ import logging import re import pkg_resources -import requests from api_teams import ApiTeams # pylint: disable=relative-import +from api_rocket_chat import ApiRocketChat # pylint: disable=relative-import from django.conf import settings from django.contrib.auth.models import User @@ -37,7 +37,7 @@ class RocketChatXBlock(XBlock, XBlockWithSettingsMixin, StudioEditableXBlockMixi default="Rocket Chat" ) email = String( - default="", scope=Scope.user_state, + default="", scope=Scope.user_info, help="Email in rocketChat", ) rocket_chat_role = String( @@ -66,11 +66,7 @@ class RocketChatXBlock(XBlock, XBlockWithSettingsMixin, StudioEditableXBlockMixi values_provider=lambda self: self.channels_enabled(), ) - team_channel = String( - default=None, - scope=Scope.user_info, - ) - salt = "HarryPotter_and_thePrisoner_of _Azkaban" + team_channel = "" # Possible editable fields editable_fields = ('channel', 'default_channel') @@ -108,7 +104,7 @@ def student_view(self, context=None): def author_view(self, context=None): """ Returns author view fragment on Studio """ # pylint: disable=unused-argument - self._private_channel("general") + self.api_rocket_chat.private_channel("general") frag = Fragment(u"Studio Runtime RocketChatXBlock") frag.add_css(self.resource_string("static/css/rocketc.css")) frag.add_javascript(self.resource_string("static/js/src/rocketc.js")) @@ -145,10 +141,9 @@ def workbench_scenarios(): ] @property - def admin_data(self): + def api_rocket_chat(self): """ - This property allows to use in the internal methods self.admin_data - as a class' field + Creates an ApiRcoketChat object """ try: user = self.xblock_settings["admin_user"] @@ -156,30 +151,22 @@ def admin_data(self): except KeyError: LOG.exception("The admin's settings has not been found") raise + api = ApiRocketChat(user, password, self.server_data["private_url_service"]) - url = "{}/{}".format(self.url_api_rocket_chat, "login") - data = {"user": user, "password": password} - headers = {"Content-type": "application/json"} - response = requests.post(url=url, json=data, headers=headers) - admin_data = {} - admin_data["auth_token"] = response.json()["data"]["authToken"] - admin_data["user_id"] = response.json()["data"]["userId"] - - LOG.info("Auth_token: %s, User_id: %s ", admin_data[ - "auth_token"], admin_data["user_id"]) + LOG.info("Api rocketChat initialize: %s ", api) - return admin_data + return api @property - def url_api_rocket_chat(self): + def server_data(self): """ - This method retunrs the rocketChat url service where someone can acces to API + This method allows to get private and public url from xblock settings """ - server_data = self.server_data - if "private_url_service" in server_data: - return "/".join([server_data["private_url_service"], "api", "v1"]) - LOG.warning("The request will be made to localhost") - return "/".join(["http://localhost:3000", "api", "v1"]) + xblock_settings = self.xblock_settings + server_data = {} + server_data["private_url_service"] = xblock_settings["private_url_service"] + server_data["public_url_service"] = xblock_settings["public_url_service"] + return server_data @property def user_data(self): @@ -203,17 +190,6 @@ def user_data(self): return user_data - @property - def server_data(self): - """ - This method allows to get private and public url from xblock settings - """ - xblock_settings = self.xblock_settings - server_data = {} - server_data["private_url_service"] = xblock_settings["private_url_service"] - server_data["public_url_service"] = xblock_settings["public_url_service"] - return server_data - @property def xblock_settings(self): """ @@ -221,6 +197,20 @@ def xblock_settings(self): """ return self.get_xblock_settings() + def channels_enabled(self): + """ + This method returns a list with the channel options + """ + if self._teams_is_enabled(): + return ["Main View", "Team Discussion", "Specific Channel"] + return ["Main View", "Specific Channel"] + + def get_groups(self): + """ + This method lists the existing groups + """ + return self.api_rocket_chat.get_groups() + def init(self): """ This method initializes the user's variables and @@ -237,256 +227,128 @@ def init(self): self._join_groups(user_id, user_data) if user_data["role"] == "instructor" and self.rocket_chat_role == "user": - self.change_role(user_id, "bot") + self.api_rocket_chat.change_role(user_id, "bot") return response else: return response['errorType'] - def search_rocket_chat_user(self, username): - """ - This method allows to get a user from RocketChat data base - """ - url_path = "{}?{}={}".format("users.info", "username", username) - - return self._request_rocket_chat("get", url_path) - def login(self, user_data): """ This method allows to get the user's authToken and id or creates a user to login in RocketChat """ - rocket_chat_user = self.search_rocket_chat_user(user_data["username"]) + api = self.api_rocket_chat + + rocket_chat_user = api.search_rocket_chat_user(user_data["username"]) LOG.info("Login method: result search user: %s", rocket_chat_user["success"]) if rocket_chat_user['success']: - data = self.create_token(user_data["username"]) + data = api.create_token(user_data["username"]) else: - response = self.create_user(user_data["anonymous_student_id"], user_data[ + response = api.create_user(user_data["anonymous_student_id"], user_data[ "email"], user_data["username"]) LOG.info("Login method: result create user : %s", response) - data = self.create_token(user_data["username"]) + + data = api.create_token(user_data["username"]) LOG.info("Login method: result create token: %s", data) return data - def create_token(self, username): - """ - This method generates a token that allows to login - """ - url_path = "users.createToken" - data = {'username': username} - return self._request_rocket_chat("post", url_path, data) - - def change_role(self, user_id, role): - """ - This method allows to change the user's role - """ - data = {"userId": user_id, "data": {"roles": [role]}} - response = self._request_rocket_chat( - "post", "users.update", data) - if response["success"]: - self.rocket_chat_role = role - - def create_user(self, name, email, username): - """ - This method creates a user with a specific name, username, email and password. - The password is a result from a SHA1 function with the name and salt. - """ - password = "{}{}".format(name, self.salt) - password = hashlib.sha1(password).hexdigest() - data = {"name": name, "email": email, - "password": password, "username": username} - return self._request_rocket_chat("post", "users.create", data) - - def add_to_course_group(self, group_name, user_id): + def _add_to_course_group(self, group_name, user_id): """ This method add the user to the default course channel """ - rocket_chat_group = self._search_rocket_chat_group(group_name) + api = self.api_rocket_chat + rocket_chat_group = api.search_rocket_chat_group(group_name) if rocket_chat_group['success']: - self._add_to_group(user_id, rocket_chat_group['group']['_id']) + api.add_to_group(user_id, rocket_chat_group['group']['_id']) else: - rocket_chat_group = self._create_group(group_name, self.user_data["username"]) + rocket_chat_group = api.create_group(group_name, self.user_data["username"]) - self.group = self._search_rocket_chat_group( # pylint: disable=attribute-defined-outside-init + self.group = api.search_rocket_chat_group( # pylint: disable=attribute-defined-outside-init group_name) - def _search_rocket_chat_group(self, room_name): - """ - This method gets a group with a specific name and returns a json with group's info - """ - url_path = "{}?{}={}".format("groups.info", "roomName", room_name) - return self._request_rocket_chat("get", url_path) - - def _add_to_group(self, user_id, room_id): - """ - This method add any user to any group - """ - url_path = "groups.invite" - data = {"roomId": room_id, "userId": user_id} - response = self._request_rocket_chat("post", url_path, data) - LOG.info("Method Add to Group: %s with this data: %s", response, data) - return response - - def _create_group(self, name, username=""): - """ - This method creates a group with a specific name. - """ - url_path = "groups.create" - data = {"name": name, "members": [username]} - response = self._request_rocket_chat("post", url_path, data) - LOG.info("Method Create Group: %s with this data: %s", response, data) - return response - - def _request_rocket_chat(self, method, url_path, data=None): - """ - This method generates a call to the RocketChat API and returns a json with the response - """ - headers = {"X-Auth-Token": self.admin_data["auth_token"], - "X-User-Id": self.admin_data["user_id"], "Content-type": "application/json"} - url = "{}/{}".format(self.url_api_rocket_chat, url_path) - if method == "post": - response = requests.post(url=url, json=data, headers=headers) - else: - response = requests.get(url=url, headers=headers) - return response.json() - - def _user_image_url(self): - """Returns an image url for the current user""" - from openedx_dependencies import get_profile_image_urls_for_user # pylint: disable=relative-import - current_user = User.objects.get(username=self.user_data["username"]) - profile_image_url = get_profile_image_urls_for_user(current_user)[ - "full"] - - if profile_image_url.startswith("http"): - return profile_image_url - - base_url = settings.LMS_ROOT_URL - image_url = "{}{}".format(base_url, profile_image_url) - LOG.info("User image url: %s ", image_url) - return image_url - - def _set_avatar(self, username): - image_url = self._user_image_url() - url_path = "users.setAvatar" - data = {"username": username, "avatarUrl": image_url} - response = self._request_rocket_chat("post", url_path, data) - LOG.info("Method set avatar: %s with this data: %s", response, data) - - def _update_user(self, user_id, username, email): - """ - This method allows to update The user data - """ - if email != self.email: - url_path = "users.update" - data = {"userId": user_id, "data": {"email": email}} - response = self._request_rocket_chat("post", url_path, data) - LOG.info("Method Update User: %s with this data: %s", response, data) - if response["success"]: - self.email = email - self._set_avatar(username) - - @XBlock.json_handler - def create_group(self, data, suffix=""): - """ - This method allows to create a group from studio - """ - # pylint: disable=unused-argument - - group_name = data["groupName"] - description = data["description"] - topic = data["topic"] - - if group_name == "" or group_name is None: - return {"success": False, "error": "Group Name is not valid"} - - group_name = group_name.replace(" ", "_") - group = self._create_group(group_name) - - if group["success"]: - self.default_channel = group_name - - if "group" in group: - group_id = group["group"]["_id"] - - self._set_description(group_id, description) - self._set_topic(group_id, topic) - - LOG.info("Method Public Create Group: %s", group) - return group - def _add_to_default_group(self, group_name, user_id): """ """ - group_info = self._search_rocket_chat_group(group_name) + api = self.api_rocket_chat + group_info = api.search_rocket_chat_group(group_name) if group_info["success"]: - self._add_to_group(user_id, group_info['group']['_id']) + api.add_to_group(user_id, group_info['group']['_id']) return True return False - def get_groups(self): + def _add_to_team_group(self, user_id, username, course_id): """ - This method lists the existing groups + Add the user to team's group in rocketChat """ - url_path = "groups.list" - method = "get" - response = self._request_rocket_chat(method, url_path) - list_groups = [] + team = self._get_team(username, course_id) + api = self.api_rocket_chat - if "groups" in response: - for group in response["groups"]: - list_groups.append(group["name"]) + if team is None: + return False - return list_groups + group_name = "-".join(["Team", team["topic_id"], team["name"]]) + group_info = api.search_rocket_chat_group(group_name) + self.team_channel = group_name - def _private_channel(self, room_name): - """ - This method changes channels from public to private - the channel's type is define as t, when t = c is a public channel - and when t = p is a private channel - """ - url_search = "{}?{}={}".format("channels.info", "roomName", room_name) - channel = self._request_rocket_chat("get", url_search) + if group_info["success"]: + response = api.add_to_group(user_id, group_info['group']['_id']) + LOG.info("Add to team group response: %s", response) + return response["success"] - if "channel" in channel and channel["channel"]["t"] == "c": - channel_id = channel["channel"]["_id"] - url_path = "channels.setType" - data = {"roomId": channel_id, "type": "p"} - self._request_rocket_chat("post", url_path, data) + response = api.create_group(group_name, username) + LOG.info("Add to team group response: %s", response) + return response["success"] - def _set_description(self, group_id, description): + def _get_team(self, username, course_id): """ - This method allows to set a description's group + This method gets the user's team """ - if description == "" or description is None: - return - - url_path = "groups.setDescription" - data = {"roomId": group_id, "description": description} - method = "post" + try: + user = self.xblock_settings["username"] + password = self.xblock_settings["password"] + client_id = self.xblock_settings["client_id"] + client_secret = self.xblock_settings["client_secret"] + except KeyError: + raise - response = self._request_rocket_chat(method, url_path, data) + server_url = settings.LMS_ROOT_URL - LOG.info("Method Set Description %s with this data: %s", response, data) + api = ApiTeams(user, password, client_id, client_secret, server_url) + team = api.get_user_team(course_id, username) + LOG.info("Get Team response: %s", team) + if team: + return team[0] + return None - def _set_topic(self, group_id, topic): + def _join_groups(self, user_id, user_data): """ - This method allows to set a topic's group + This methodd add the user to the diferent channels """ - if topic == "" or topic is None: - return + default_channel = self.default_channel + channel = self.channel + api = self.api_rocket_chat + + if channel == "Team Discussion" and self._teams_is_enabled(): + self.ui_is_block = self._add_to_team_group( + user_id, user_data["username"], user_data["course_id"]) + + elif channel == "Specific Channel": + self.ui_is_block = self._add_to_default_group(default_channel, user_id) - url_path = "groups.setTopic" - data = {"roomId": group_id, "topic": topic} - method = "post" + else: + self.ui_is_block = False + self._add_to_course_group(user_data["course"], user_id) - response = self._request_rocket_chat(method, url_path, data) + if (user_data["email"]!= self.email): + self.email = api.update_user(user_id, user_data["username"], user_data["email"]) - LOG.info("Method Set Topic: %s with this data: %s", response, data) + api.set_avatar(user_data["username"], self._user_image_url()) def _teams_is_enabled(self): """ @@ -506,73 +368,48 @@ def _teams_is_enabled(self): return False - def _get_team(self, username, course_id): - """ - This method gets the user's team - """ - try: - user = self.xblock_settings["username"] - password = self.xblock_settings["password"] - client_id = self.xblock_settings["client_id"] - client_secret = self.xblock_settings["client_secret"] - except KeyError: - raise + def _user_image_url(self): + """Returns an image url for the current user""" + from openedx_dependencies import get_profile_image_urls_for_user # pylint: disable=relative-import + current_user = User.objects.get(username=self.user_data["username"]) + profile_image_url = get_profile_image_urls_for_user(current_user)[ + "full"] - server_url = settings.LMS_ROOT_URL + if profile_image_url.startswith("http"): + return profile_image_url - api = ApiTeams(user, password, client_id, client_secret, server_url) - team = api.get_user_team(course_id, username) - LOG.info("Get Team response: %s", team) - if team: - return team[0] - return None + base_url = settings.LMS_ROOT_URL + image_url = "{}{}".format(base_url, profile_image_url) + LOG.info("User image url: %s ", image_url) + return image_url - def _add_to_team_group(self, user_id, username, course_id): + @XBlock.json_handler + def create_group(self, data, suffix=""): """ - Add the user to team's group in rocketChat + This method allows to create a group from studio """ - team = self._get_team(username, course_id) - - if team is None: - return False - - group_name = "-".join(["Team", team["topic_id"], team["name"]]) - group_info = self._search_rocket_chat_group(group_name) - self.team_channel = group_name + # pylint: disable=unused-argument - if group_info["success"]: - response = self._add_to_group(user_id, group_info['group']['_id']) - LOG.info("Add to team group response: %s", response) - return response["success"] + api = self.api_rocket_chat - response = self._create_group(group_name, username) - LOG.info("Add to team group response: %s", response) - return response["success"] + group_name = data["groupName"] + description = data["description"] + topic = data["topic"] - def _join_groups(self, user_id, user_data): - """ - This methodd add the user to the diferent channels - """ - default_channel = self.default_channel - channel = self.channel + if group_name == "" or group_name is None: + return {"success": False, "error": "Group Name is not valid"} - if channel == "Team Discussion" and self._teams_is_enabled(): - self.ui_is_block = self._add_to_team_group( - user_id, user_data["username"], user_data["course_id"]) + group_name = group_name.replace(" ", "_") + group = api.create_group(group_name) - elif channel == "Specific Channel": - self.ui_is_block = self._add_to_default_group(default_channel, user_id) + if group["success"]: + self.default_channel = group_name - else: - self.ui_is_block = False - self.add_to_course_group(user_data["course"], user_id) + if "group" in group: + group_id = group["group"]["_id"] - self._update_user(user_id, user_data["username"], user_data["email"]) + api.set_description(group_id, description) + api.set_topic(group_id, topic) - def channels_enabled(self): - """ - This method returns a list with the channel options - """ - if self._teams_is_enabled(): - return ["Main View", "Team Discussion", "Specific Channel"] - return ["Main View", "Specific Channel"] + LOG.info("Method Public Create Group: %s", group) + return group From 6aef94ff2b05e9932923f576024623bfd136cdbc Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Fri, 27 Apr 2018 10:32:25 -0500 Subject: [PATCH 2/8] refactor --- rocketc/api_rocket_chat.py | 4 +- rocketc/openedx_dependencies.py | 1 - rocketc/rocketc.py | 18 +- rocketc/static/html/rocketc.html | 6 +- rocketc/tests/test_rocketChat.py | 480 +++++++++++++++++++++---------- 5 files changed, 349 insertions(+), 160 deletions(-) diff --git a/rocketc/api_rocket_chat.py b/rocketc/api_rocket_chat.py index 065a6374..fcdc744e 100644 --- a/rocketc/api_rocket_chat.py +++ b/rocketc/api_rocket_chat.py @@ -2,8 +2,8 @@ This file contains the class with the necessary methos to use rocketchat """ import hashlib -import requests import logging +import requests LOG = logging.getLogger(__name__) @@ -184,7 +184,7 @@ def set_topic(self, group_id, topic): LOG.info("Method Set Topic: %s with this data: %s", response, data) - def update_user(self, user_id, username, email): + def update_user(self, user_id, email): """ This method allows to update The user data """ diff --git a/rocketc/openedx_dependencies.py b/rocketc/openedx_dependencies.py index 26597263..2d0f2430 100644 --- a/rocketc/openedx_dependencies.py +++ b/rocketc/openedx_dependencies.py @@ -4,5 +4,4 @@ # pylint: disable=import-error # pylint: disable=unused-import from openedx.core.djangoapps.user_api.accounts.image_helpers import get_profile_image_urls_for_user -from lms.djangoapps.teams.models import CourseTeam, CourseTeamMembership from xmodule.modulestore.django import modulestore diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index 5478ed95..393f0c17 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -1,7 +1,6 @@ """ TO-DO: Write a description of what this XBlock is. """ -import hashlib import logging import re import pkg_resources @@ -225,6 +224,7 @@ def init(self): user_id = response['userId'] self._join_groups(user_id, user_data) + self._update_user(user_id, user_data) if user_data["role"] == "instructor" and self.rocket_chat_role == "user": self.api_rocket_chat.change_role(user_id, "bot") @@ -321,6 +321,7 @@ def _get_team(self, username, course_id): api = ApiTeams(user, password, client_id, client_secret, server_url) team = api.get_user_team(course_id, username) + print team LOG.info("Get Team response: %s", team) if team: return team[0] @@ -332,7 +333,6 @@ def _join_groups(self, user_id, user_data): """ default_channel = self.default_channel channel = self.channel - api = self.api_rocket_chat if channel == "Team Discussion" and self._teams_is_enabled(): self.ui_is_block = self._add_to_team_group( @@ -345,11 +345,6 @@ def _join_groups(self, user_id, user_data): self.ui_is_block = False self._add_to_course_group(user_data["course"], user_id) - if (user_data["email"]!= self.email): - self.email = api.update_user(user_id, user_data["username"], user_data["email"]) - - api.set_avatar(user_data["username"], self._user_image_url()) - def _teams_is_enabled(self): """ This method verifies if teams are available @@ -368,6 +363,15 @@ def _teams_is_enabled(self): return False + def _update_user(self, user_id, user_data): + """ + """ + api = self.api_rocket_chat + if user_data["email"] != self.email: + self.email = api.update_user(user_id, user_data["email"]) + + api.set_avatar(user_data["username"], self._user_image_url()) + def _user_image_url(self): """Returns an image url for the current user""" from openedx_dependencies import get_profile_image_urls_for_user # pylint: disable=relative-import diff --git a/rocketc/static/html/rocketc.html b/rocketc/static/html/rocketc.html index dab5023e..7980ae08 100644 --- a/rocketc/static/html/rocketc.html +++ b/rocketc/static/html/rocketc.html @@ -1,8 +1,8 @@
- {% if ui_is_block%} + {% if ui_is_block %} - {% if "authToken" in response%} + {% if "authToken" in response %}
@@ -13,7 +13,7 @@ {% else %} - {% if "authToken" in response%} + {% if "authToken" in response %}
diff --git a/rocketc/tests/test_rocketChat.py b/rocketc/tests/test_rocketChat.py index 68c6eef6..499cb292 100644 --- a/rocketc/tests/test_rocketChat.py +++ b/rocketc/tests/test_rocketChat.py @@ -1,7 +1,10 @@ import unittest import hashlib -from mock import MagicMock, patch +import json + +from mock import MagicMock, patch, PropertyMock from rocketc.rocketc import RocketChatXBlock +from rocketc.api_rocket_chat import ApiRocketChat class TestRocketChat(unittest.TestCase): @@ -17,39 +20,57 @@ def setUp(self): self.runtime_mock, scope_ids=scope_ids_mock) self.block.email = "email" - @patch('rocketc.rocketc.RocketChatXBlock.url_api_rocket_chat') - @patch('rocketc.rocketc.RocketChatXBlock.admin_data') - def test_request_rocket_chat(self, mock_admin, mock_url): - """Test for the request rocket chat method """ - users = [{ - "user": { - "_id": "BsNr28znDkG8aeo7W", - "createdAt": "2016-09-13T14:57:56.037Z", - }, - "success": "true", - }] + modules = { + 'rocketc.openedx_dependencies' : MagicMock(), + } - info = [{ - "success": "true", - "info": { - "version": "0.47.0-develop" - } - }] + self.module_patcher = patch.dict('sys.modules', modules) + self.module_patcher.start() - with patch('rocketc.rocketc.requests.post') as mock_post: - mock_post.return_value.json.return_value = users - data_post = self.block._request_rocket_chat("post", "users.create") + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_add_to_course_group(self, mock_api_rocket): + """Test for the add course group method""" + group_name = "test_group" + user_id = "test_user_id" + data = {'success': True, 'group': {'_id': "test_group_id"}} - with patch('rocketc.rocketc.requests.get') as mock_get: - mock_get.return_value.json.return_value = info - data_get = self.block._request_rocket_chat("get", "info") - self.assertEqual(data_post, users) - self.assertEqual(data_get, info) + mock_api_rocket.search_rocket_chat_group.return_value = data + self.block._add_to_course_group(group_name, user_id) + mock_api_rocket.add_to_group.assert_called_with(user_id, data['group']['_id']) + + with patch('rocketc.rocketc.RocketChatXBlock.user_data', new_callable=PropertyMock) as mock_user: + mock_user.return_value = {"username": "test_user_name"} + data['success'] = False + mock_api_rocket.search_rocket_chat_group.return_value = data + self.block._add_to_course_group(group_name, user_id) + mock_api_rocket.create_group.assert_called_with(group_name, "test_user_name") + + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_create_group(self, mock_api_rocket): + """test create group""" + + data = {"groupName": "", "description": "test:description", "topic": "test_topic"} + + mock_request = MagicMock( method="POST", body=json.dumps(data)) + + result = self.block.create_group(mock_request) + + self.assertEqual(json.loads(result.body)["error"], "Group Name is not valid") + + data = {"groupName": "test_group", "description": "test:description", "topic": "test_topic"} + + mock_request = MagicMock( method="POST", body=json.dumps(data)) + mock_api_rocket.create_group.return_value = {"success": True, "group": {"_id": "1234"}} - @patch('rocketc.rocketc.RocketChatXBlock._update_user') - @patch('rocketc.rocketc.RocketChatXBlock.create_token') - def test_login(self, mock_token, mock_update_user): + result = self.block.create_group(mock_request) + + mock_api_rocket.create_group.assert_called_with(data["groupName"]) + self.assertEqual(self.block.default_channel, data["groupName"]) + self.assertEqual(json.loads(result.body), {"success": True, "group": {"_id": "1234"}}) + + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_login(self, mock_api_rocket): """Test for the login method""" test_data = {"username": "test_user_name", @@ -58,76 +79,164 @@ def test_login(self, mock_token, mock_update_user): "course": "test_course", "role": "test_role" } - - mock_token.return_value = {'success': True} + mock_api_rocket.create_token.return_value = {'success': True} success = {'success': True} - with patch('rocketc.rocketc.RocketChatXBlock.search_rocket_chat_user', return_value=success): - result_if = self.block.login(test_data) - mock_token.assert_called_with(test_data['username']) + + result_if = self.block.login(test_data) + mock_api_rocket.create_token.assert_called_with(test_data['username']) success['success'] = False - with patch('rocketc.rocketc.RocketChatXBlock.search_rocket_chat_user', return_value=success): - with patch('rocketc.rocketc.RocketChatXBlock.create_user'): - result_else = self.block.login(test_data) - mock_token.assert_called_with(test_data['username']) + + mock_api_rocket.search_rocket_chat_user.return_value= success + + result_else = self.block.login(test_data) + mock_api_rocket.create_user.assert_called_with(test_data["anonymous_student_id"], test_data[ + "email"], test_data["username"]) + mock_api_rocket.create_token.assert_called_with(test_data['username']) self.assertTrue(result_if['success']) self.assertTrue(result_else['success']) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_search_rocket_chat_user(self, mock_request): - """Test for the search user method""" - method = "get" + # @patch('rocketc.rocketc.api_teams.ApiTeams') + # def test_get_team(self, mock_teams): + # """ + # This method gets the user's team + # """ + # username = "test_user_name" + # course_id = " test_course_id" + # mock_teams.get_user_team.return_value = [{"test_team"}] + + # with patch('rocketc.rocketc.RocketChatXBlock.xblock_settings', new_callable=PropertyMock) as mock_settings: + # mock_settings.return_value = {"username": "test_user_name", "password": "test_password", + # "client_id": "test_id", "client_secret": "test_secret"} + # self.assertEqual(self.block._get_team(username, course_id),{"test_team"}) + # mock_teams.get_user_team.return_value = [] + # self.assertIsNone(self.block._get_team(username, course_id)) + + def test_join_groups(self): + """ + Test the method join groups + """ + + user_id = "test_user_id" + user_data = {"course_id": "test_course_id", "username": "test_user_name", "course": "test_course"} + self.block.default_channel = "test_default_channel" + self.block.channel = "Team Discussion" + + with patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled', return_value=True ): + with patch('rocketc.rocketc.RocketChatXBlock._add_to_team_group', return_value=True ): + self.block._join_groups(user_id, user_data) + self.assertTrue(self.block.ui_is_block) + + self.block.channel = "Specific Channel" + + with patch('rocketc.rocketc.RocketChatXBlock._add_to_default_group', return_value=True ): + self.block._join_groups(user_id, user_data) + self.assertTrue(self.block.ui_is_block) + + self.block.channel = "" + + with patch('rocketc.rocketc.RocketChatXBlock._add_to_course_group'): + self.block._join_groups(user_id, user_data) + self.assertFalse(self.block.ui_is_block) + + @patch('rocketc.rocketc.RocketChatXBlock._user_image_url') + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_update_user(self, mock_api_rocket, mock_url): + """Test update user """ + user_data = {"email": "test_email", "username": "test_user_name"} + user_id = "test_user_id" + + mock_url.return_value = "test_url" + + self.block._update_user(user_id, user_data) + mock_api_rocket.update_user.assert_called_with(user_id, user_data["email"]) + mock_api_rocket.set_avatar.assert_called_with(user_data["username"], "test_url") + + # @patch('django.contrib.auth.models.User') + # @patch('rocketc.openedx_dependencies.get_profile_image_urls_for_user') + # def test_user_image_url(self, morck_url, mock_user): + # """test user_imae_url""" + # moock_url.return_value = "http://url_test" + # result = self.block._user_image_url() + # self.assertEqual(result, "http://url_test") + + # moock_url = "url_test" + + # result = self.block._user_image_url() + # self.assertEqual(result, "http://url_test") + + +class TestApiRocketChat(unittest.TestCase): + """ Unit tests for ApiRocketChat Xblock""" + + def setUp(self): + """Set up general variables""" + + user = "test_user" + password = "test_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_to_group(self, mock_request): + """Test for the add group method""" + method = "post" success = {'success': True} - username = "test_user_name" + + user_id = "test_user_id" + room_id = "test_room_id" + mock_request.return_value = success - url_path = "{}?{}={}".format("users.info", "username", username) + url_path = "groups.invite" - response = self.block.search_rocket_chat_user(username) + data = {"roomId": room_id, "userId": user_id} - mock_request.assert_called_with(method, url_path) + response = self.api.add_to_group(user_id, room_id) + mock_request.assert_called_with(method, url_path, data) self.assertTrue(response['success']) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_search_rocket_chat_group(self, mock_request): - """Test for the search group method""" - method = "get" + @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') + def test_change_role(self, mock_request): + """Test for chage role method""" + method = "post" success = {'success': True} - room_name = "test_room_name" + + user_id = "test_user_id" + role = "test_role" + mock_request.return_value = success - url_path = "{}?{}={}".format("groups.info", "roomName", room_name) + url_path = "users.update" - response = self.block._search_rocket_chat_group(room_name) + data = {"userId": user_id, "data": {"roles": [role]}} - mock_request.assert_called_with(method, url_path) - self.assertTrue(response['success']) + self.api.change_role(user_id, role) + mock_request.assert_called_with(method, url_path, data) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_create_user(self, mock_request): - """Test for the create user method""" + @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} - email = "test_email" - username = "test_user_name" name = "test_name" - salt = "HarryPotter_and_thePrisoner_of _Azkaban" + username = "test_user_name" 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} + url_path = "groups.create" - response = self.block.create_user(name, email, username) + 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) - self.assertTrue(response['success']) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') + @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') def test_create_token(self, mock_request): """Test for the create token method""" method = "post" @@ -140,104 +249,203 @@ def test_create_token(self, mock_request): data = {'username': username} - response = self.block.create_token(username) + response = self.api.create_token(username) mock_request.assert_called_with(method, url_path, data) self.assertTrue(response['success']) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_create_group(self, mock_request): - """Test for the create group method""" + @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} - name = "test_name" + email = "test_email" username = "test_user_name" + name = "test_name" + salt = "HarryPotter_and_thePrisoner_of _Azkaban" mock_request.return_value = success - url_path = "groups.create" + url_path = "users.create" - data = {'name': name, "members":[username]} + 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) - response = self.block._create_group(name, username) - self.assertEquals(response, success) mock_request.assert_called_with(method, url_path, data) + self.assertTrue(response['success']) - @patch('rocketc.rocketc.RocketChatXBlock.user_data') - @patch('rocketc.rocketc.RocketChatXBlock._add_to_group') - def test_add_to_course_group(self, mock_add_to_group, mock_user): - """Test for the add course group method""" - group_name = "test_group" - user_id = "test_user_id" - data = {'success': True, 'group': {'_id': "test_group_id"}} - with patch('rocketc.rocketc.RocketChatXBlock._search_rocket_chat_group', return_value=data): - self.block.add_to_course_group(group_name, user_id) - mock_add_to_group.assert_called_with(user_id, data['group']['_id']) + @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" - data['success'] = False - with patch('rocketc.rocketc.RocketChatXBlock._search_rocket_chat_group', return_value=data): - with patch('rocketc.rocketc.RocketChatXBlock._create_group', return_value=data): - self.block.add_to_course_group(group_name, user_id) - mock_add_to_group.assert_called_with(user_id, data['group']['_id']) + groups = {"groups":[{"name": "group1"}]} + mock_request.return_value = groups - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_add_to_group(self, mock_request): - """Test for the add group method""" - method = "post" - success = {'success': True} + return_value = self.api.get_groups() - user_id = "test_user_id" - room_id = "test_room_id" + mock_request.assert_called_with(method, url_path) + self.assertIn("group1", return_value) - mock_request.return_value = success - url_path = "groups.invite" + mock_request.return_value = {} - data = {"roomId": room_id, "userId": user_id} + return_value = self.api.get_groups() - response = self.block._add_to_group(user_id, room_id) - mock_request.assert_called_with(method, url_path, data) - self.assertTrue(response['success']) + self.assertFalse(return_value) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_change_role(self, mock_request): - """Test for chage role method""" + @patch('rocketc.api_rocket_chat.requests.post') + def test_login(self, mock_request): + """""" + user = "test_user" + password = "test_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_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.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 = "{}?{}={}".format("groups.info", "roomName", room_name) - user_id = "test_user_id" - role = "test_role" + response = self.api.search_rocket_chat_group(room_name) + + mock_request.assert_called_with(method, url_path) + 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 = "test_user_name" mock_request.return_value = success - url_path = "users.update" + url_path = "{}?{}={}".format("users.info", "username", username) - data = {"userId": user_id, "data": {"roles": [role]}} + response = self.api.search_rocket_chat_user(username) - self.block.change_role(user_id, role) - mock_request.assert_called_with(method, url_path, data) + mock_request.assert_called_with(method, url_path) + self.assertTrue(response['success']) - @patch('rocketc.rocketc.RocketChatXBlock._user_image_url') - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') - def test_set_avatar(self, mock_request, mock_user_image_url): + @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 = "test_user_name" url ="test_url" - mock_user_image_url.return_value = url url_path = "users.setAvatar" data = {"username": username, "avatarUrl": url} - self.block._set_avatar(username) + self.api.set_avatar(username, url) mock_request.assert_called_with(method, url_path, data) - @patch('rocketc.rocketc.RocketChatXBlock._request_rocket_chat') + @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') + def test_set_description(self, mock_request): + """ + This method tests the method set_description + """ + description = None + group_id = "test_id" + + return_none = self.api.set_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_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_topic(self, mock_request): + """ + This method test the method set_topic + """ + topic = None + group_id = "test_id" + + return_none = self.api.set_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_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" - username = "test_user_name" email = "test_email" mock_request.return_value = success @@ -245,29 +453,7 @@ def test_update_user(self, mock_request): data = {"userId": user_id, "data": {"email": email }} - with patch('rocketc.rocketc.RocketChatXBlock._set_avatar'): - self.block._update_user(user_id, username, email) + new_email = self.api.update_user(user_id, email) - self.assertEqual(self.block.email, email) + self.assertEqual(new_email, email) mock_request.assert_called_with(method, url_path, data) - - @patch('rocketc.rocketc.RocketChatXBlock._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.block.get_groups() - - mock_request.assert_called_with(method, url_path) - self.assertIn("group1", return_value) - - mock_request.return_value = {} - - return_value = self.block.get_groups() - - self.assertFalse(return_value) - From 045305744e8f3eced69a8460dc416ce5acdf4714 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Mon, 30 Apr 2018 12:49:37 -0500 Subject: [PATCH 3/8] Add test --- rocketc/rocketc.py | 1 - rocketc/tests/test_rocketChat.py | 219 ++++++++++++++++++++++--------- 2 files changed, 160 insertions(+), 60 deletions(-) diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index 393f0c17..dcd71b0f 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -321,7 +321,6 @@ def _get_team(self, username, course_id): api = ApiTeams(user, password, client_id, client_secret, server_url) team = api.get_user_team(course_id, username) - print team LOG.info("Get Team response: %s", team) if team: return team[0] diff --git a/rocketc/tests/test_rocketChat.py b/rocketc/tests/test_rocketChat.py index 499cb292..3bbfc47b 100644 --- a/rocketc/tests/test_rocketChat.py +++ b/rocketc/tests/test_rocketChat.py @@ -2,6 +2,7 @@ import hashlib import json +from django.test.utils import override_settings from mock import MagicMock, patch, PropertyMock from rocketc.rocketc import RocketChatXBlock from rocketc.api_rocket_chat import ApiRocketChat @@ -20,11 +21,11 @@ def setUp(self): self.runtime_mock, scope_ids=scope_ids_mock) self.block.email = "email" - modules = { - 'rocketc.openedx_dependencies' : MagicMock(), + self.modules = { + 'rocketc.openedx_dependencies': MagicMock(), } - self.module_patcher = patch.dict('sys.modules', modules) + self.module_patcher = patch.dict('sys.modules', self.modules) self.module_patcher.start() @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') @@ -34,51 +35,141 @@ def test_add_to_course_group(self, mock_api_rocket): user_id = "test_user_id" data = {'success': True, 'group': {'_id': "test_group_id"}} - mock_api_rocket.search_rocket_chat_group.return_value = data self.block._add_to_course_group(group_name, user_id) - mock_api_rocket.add_to_group.assert_called_with(user_id, data['group']['_id']) + mock_api_rocket.add_to_group.assert_called_with( + user_id, data['group']['_id']) with patch('rocketc.rocketc.RocketChatXBlock.user_data', new_callable=PropertyMock) as mock_user: mock_user.return_value = {"username": "test_user_name"} data['success'] = False mock_api_rocket.search_rocket_chat_group.return_value = data self.block._add_to_course_group(group_name, user_id) - mock_api_rocket.create_group.assert_called_with(group_name, "test_user_name") + mock_api_rocket.create_group.assert_called_with( + group_name, "test_user_name") + + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_add_to_default_group(self, mock_api_rocket): + """ + Test the method add to default group + """ + group_name = "test_group_name" + user_id = "test_user_id" + + mock_api_rocket.search_rocket_chat_group.return_value = {"success": True, "group": {"_id": "1234"}} + + self.assertTrue(self.block._add_to_default_group(group_name, user_id)) + mock_api_rocket.add_to_group.assert_called_with(user_id, "1234") + + mock_api_rocket.search_rocket_chat_group.return_value = {"success": False} + + self.assertFalse(self.block._add_to_default_group(group_name, user_id)) + mock_api_rocket.add_to_group.assert_called_with(user_id, "1234") + + @patch('rocketc.rocketc.RocketChatXBlock._get_team') + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_add_to_team_group(self, mock_api_rocket, mock_get_team): + """ + test method add to team group + """ + user_id = "test_user_id" + username = "test_user_name" + course_id = "test_course_id" + + mock_get_team.return_value = None + + self.assertFalse(self.block._add_to_team_group(user_id, username, course_id)) + + mock_get_team.return_value = {"name": "name", "topic_id": "test"} + mock_api_rocket.search_rocket_chat_group.return_value = {"success": True, "group": {"_id": "1234"}} + + self.block._add_to_team_group(user_id, username, course_id) + self.assertEqual(self.block.team_channel, "Team-test-name") + + mock_api_rocket.add_to_group.assert_called_with(user_id, "1234") + + mock_api_rocket.search_rocket_chat_group.return_value = {"success": False} + self.block._add_to_team_group(user_id, username, course_id) + + mock_api_rocket.create_group.assert_called_with("Team-test-name", username) + + @patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled') + def test_channels_enabled(self, mock_team): + """ + Test the method channels is enabled + """ + output = ["Main View", "Team Discussion", "Specific Channel"] + mock_team.return_value = True + self.assertEqual(self.block.channels_enabled(), output) + + output = ["Main View", "Specific Channel"] + mock_team.return_value = False + self.assertEqual(self.block.channels_enabled(), output) @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') def test_create_group(self, mock_api_rocket): """test create group""" - data = {"groupName": "", "description": "test:description", "topic": "test_topic"} + data = {"groupName": "", "description": "test:description", + "topic": "test_topic"} - mock_request = MagicMock( method="POST", body=json.dumps(data)) + mock_request = MagicMock(method="POST", body=json.dumps(data)) result = self.block.create_group(mock_request) - self.assertEqual(json.loads(result.body)["error"], "Group Name is not valid") + self.assertEqual(json.loads(result.body)[ + "error"], "Group Name is not valid") - data = {"groupName": "test_group", "description": "test:description", "topic": "test_topic"} + data = {"groupName": "test_group", + "description": "test:description", "topic": "test_topic"} - mock_request = MagicMock( method="POST", body=json.dumps(data)) - mock_api_rocket.create_group.return_value = {"success": True, "group": {"_id": "1234"}} + mock_request = MagicMock(method="POST", body=json.dumps(data)) + mock_api_rocket.create_group.return_value = { + "success": True, "group": {"_id": "1234"}} result = self.block.create_group(mock_request) mock_api_rocket.create_group.assert_called_with(data["groupName"]) self.assertEqual(self.block.default_channel, data["groupName"]) - self.assertEqual(json.loads(result.body), {"success": True, "group": {"_id": "1234"}}) + self.assertEqual(json.loads(result.body), { + "success": True, "group": {"_id": "1234"}}) + + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + @patch('rocketc.rocketc.RocketChatXBlock._update_user') + @patch('rocketc.rocketc.RocketChatXBlock._join_groups') + @patch('rocketc.rocketc.RocketChatXBlock.login') + def test_init(self, mock_login, mock_join_groups, mock_update_user, mock_api_rocket): + """ + Test the method to initialize the xblock + """ + with patch('rocketc.rocketc.RocketChatXBlock.user_data', new_callable=PropertyMock) as mock_user_data: + user_data = {"role": "instructor"} + mock_user_data.return_value = user_data + user_id = "test_user_id" + data = {"userId": user_id} + response_login = {"success": True, "data": data} + mock_login.return_value = response_login + self.block.rocket_chat_role = "user" + + self.assertEqual(self.block.init(), data) + mock_join_groups.assert_called_with(user_id, user_data) + mock_update_user.assert_called_with(user_id, user_data) + mock_api_rocket.change_role.assert_called_with(user_id, "bot") + + mock_login.return_value = {"success": False, "errorType": "test_error"} + + self.assertEqual(self.block.init(), "test_error") @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') def test_login(self, mock_api_rocket): """Test for the login method""" test_data = {"username": "test_user_name", - "anonymous_student_id": "test_anonymous_student_id", - "email": "test_email", - "course": "test_course", - "role": "test_role" - } + "anonymous_student_id": "test_anonymous_student_id", + "email": "test_email", + "course": "test_course", + "role": "test_role" + } mock_api_rocket.create_token.return_value = {'success': True} success = {'success': True} @@ -87,32 +178,33 @@ def test_login(self, mock_api_rocket): success['success'] = False - mock_api_rocket.search_rocket_chat_user.return_value= success + mock_api_rocket.search_rocket_chat_user.return_value = success result_else = self.block.login(test_data) mock_api_rocket.create_user.assert_called_with(test_data["anonymous_student_id"], test_data[ - "email"], test_data["username"]) + "email"], test_data["username"]) mock_api_rocket.create_token.assert_called_with(test_data['username']) - self.assertTrue(result_if['success']) self.assertTrue(result_else['success']) - # @patch('rocketc.rocketc.api_teams.ApiTeams') - # def test_get_team(self, mock_teams): - # """ - # This method gets the user's team - # """ - # username = "test_user_name" - # course_id = " test_course_id" - # mock_teams.get_user_team.return_value = [{"test_team"}] - - # with patch('rocketc.rocketc.RocketChatXBlock.xblock_settings', new_callable=PropertyMock) as mock_settings: - # mock_settings.return_value = {"username": "test_user_name", "password": "test_password", - # "client_id": "test_id", "client_secret": "test_secret"} - # self.assertEqual(self.block._get_team(username, course_id),{"test_team"}) - # mock_teams.get_user_team.return_value = [] - # self.assertIsNone(self.block._get_team(username, course_id)) + @override_settings(LMS_ROOT_URL="http://127.0.0.1") + @patch('rocketc.rocketc.ApiTeams') + def test_get_team(self, mock_teams): + """ + This method gets the user's team + """ + username = "test_user_name" + course_id = " test_course_id" + mock_teams.return_value.get_user_team.return_value = ["test_team"] + + with patch('rocketc.rocketc.RocketChatXBlock.xblock_settings', new_callable=PropertyMock) as mock_settings: + mock_settings.return_value = {"username": "test_user_name", "password": "test_password", + "client_id": "test_id", "client_secret": "test_secret"} + self.assertEqual(self.block._get_team( + username, course_id), "test_team") + mock_teams.return_value.get_user_team.return_value = [] + self.assertIsNone(self.block._get_team(username, course_id)) def test_join_groups(self): """ @@ -120,18 +212,19 @@ def test_join_groups(self): """ user_id = "test_user_id" - user_data = {"course_id": "test_course_id", "username": "test_user_name", "course": "test_course"} + user_data = {"course_id": "test_course_id", + "username": "test_user_name", "course": "test_course"} self.block.default_channel = "test_default_channel" self.block.channel = "Team Discussion" - with patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled', return_value=True ): - with patch('rocketc.rocketc.RocketChatXBlock._add_to_team_group', return_value=True ): + with patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled', return_value=True): + with patch('rocketc.rocketc.RocketChatXBlock._add_to_team_group', return_value=True): self.block._join_groups(user_id, user_data) self.assertTrue(self.block.ui_is_block) self.block.channel = "Specific Channel" - with patch('rocketc.rocketc.RocketChatXBlock._add_to_default_group', return_value=True ): + with patch('rocketc.rocketc.RocketChatXBlock._add_to_default_group', return_value=True): self.block._join_groups(user_id, user_data) self.assertTrue(self.block.ui_is_block) @@ -151,21 +244,27 @@ def test_update_user(self, mock_api_rocket, mock_url): mock_url.return_value = "test_url" self.block._update_user(user_id, user_data) - mock_api_rocket.update_user.assert_called_with(user_id, user_data["email"]) - mock_api_rocket.set_avatar.assert_called_with(user_data["username"], "test_url") + mock_api_rocket.update_user.assert_called_with( + user_id, user_data["email"]) + mock_api_rocket.set_avatar.assert_called_with( + user_data["username"], "test_url") - # @patch('django.contrib.auth.models.User') - # @patch('rocketc.openedx_dependencies.get_profile_image_urls_for_user') - # def test_user_image_url(self, morck_url, mock_user): - # """test user_imae_url""" - # moock_url.return_value = "http://url_test" - # result = self.block._user_image_url() - # self.assertEqual(result, "http://url_test") + @override_settings(LMS_ROOT_URL="http://127.0.0.1/") + @patch('rocketc.rocketc.User') + @patch('rocketc.rocketc.RocketChatXBlock.user_data') + def test_user_image_url(self, mock_user_data, mock_user): + """test user_imae_url""" + mock_user.return_value = MagicMock() + self.modules[ + 'rocketc.openedx_dependencies'].get_profile_image_urls_for_user.return_value = {"full": "http://url_test"} + result = self.block._user_image_url() + self.assertEqual(result, "http://url_test") - # moock_url = "url_test" + self.modules[ + 'rocketc.openedx_dependencies'].get_profile_image_urls_for_user.return_value = {"full": "url_test"} - # result = self.block._user_image_url() - # self.assertEqual(result, "http://url_test") + result = self.block._user_image_url() + self.assertEqual(result, "http://127.0.0.1/url_test") class TestApiRocketChat(unittest.TestCase): @@ -181,7 +280,7 @@ def setUp(self): 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) + self.api = ApiRocketChat(user, password, server_url) @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') def test_add_to_group(self, mock_request): @@ -230,7 +329,7 @@ def test_create_group(self, mock_request): mock_request.return_value = success url_path = "groups.create" - data = {'name': name, "members":[username]} + data = {'name': name, "members": [username]} response = self.api.create_group(name, username) self.assertEquals(response, success) @@ -284,7 +383,7 @@ def test_get_groups(self, mock_request): method = "get" url_path = "groups.list" - groups = {"groups":[{"name": "group1"}]} + groups = {"groups": [{"name": "group1"}]} mock_request.return_value = groups return_value = self.api.get_groups() @@ -307,11 +406,13 @@ def test_login(self, mock_request): 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"}} + 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) + 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") @@ -389,7 +490,7 @@ def test_set_avatar(self, mock_request): """Test the method for set the avatar in RocketChat""" method = "post" username = "test_user_name" - url ="test_url" + url = "test_url" url_path = "users.setAvatar" @@ -451,7 +552,7 @@ def test_update_user(self, mock_request): mock_request.return_value = success url_path = "users.update" - data = {"userId": user_id, "data": {"email": email }} + data = {"userId": user_id, "data": {"email": email}} new_email = self.api.update_user(user_id, email) From c95d7b96b7cc7a41ab8be749120c472175e96428 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Mon, 30 Apr 2018 16:31:22 -0500 Subject: [PATCH 4/8] Addressing pull request comments --- rocketc/api_rocket_chat.py | 31 ++++++----- rocketc/api_teams.py | 19 +++---- rocketc/rocketc.py | 47 +++++++++-------- rocketc/tests/test_rocketChat.py | 90 ++++++++++++++++---------------- 4 files changed, 96 insertions(+), 91 deletions(-) diff --git a/rocketc/api_rocket_chat.py b/rocketc/api_rocket_chat.py index fcdc744e..2a46b91e 100644 --- a/rocketc/api_rocket_chat.py +++ b/rocketc/api_rocket_chat.py @@ -20,7 +20,7 @@ def __init__(self, user, password, server_url="http://127.0.0.1:3000"): self._login(user, password) def _login(self, user, password): - """""" + """This method defines the headers with the authToken and userId""" url = "/".join([self.server_url, self.API_PATH, "login"]) data = {"user": user, "password": password} response = requests.post(url=url, json=data, headers=self.headers) @@ -32,7 +32,7 @@ def _login(self, user, password): LOG.info("Auth_token: %s, User_id: %s ", self.headers[ "X-Auth-Token"], self.headers["X-User-Id"]) - def _request_rocket_chat(self, method, url_path, data=None): + def _request_rocket_chat(self, method, url_path, data=None, payload=None): """ This method generates a call to the RocketChat API and returns a json with the response """ @@ -41,11 +41,11 @@ def _request_rocket_chat(self, method, url_path, data=None): response = requests.post(url=url, json=data, headers=self.headers) LOG.info("Request rocketChat status code = %s", response.status_code) else: - response = requests.get(url=url, headers=self.headers) + response = requests.get(url=url, headers=self.headers, params=payload) LOG.info("Request rocketChat status code = %s", response.status_code) return response.json() - def add_to_group(self, user_id, room_id): + def add_user_to_group(self, user_id, room_id): """ This method add any user to any group """ @@ -55,7 +55,7 @@ def add_to_group(self, user_id, room_id): LOG.info("Method Add to Group: %s with this data: %s", response, data) return response - def change_role(self, user_id, role): + def change_user_role(self, user_id, role): """ This method allows to change the user's role """ @@ -115,14 +115,15 @@ def get_groups(self): return list_groups - def private_channel(self, room_name): + def convert_to_private_channel(self, room_name): """ This method changes channels from public to private the channel's type is define as t, when t = c is a public channel and when t = p is a private channel """ - url_search = "{}?{}={}".format("channels.info", "roomName", room_name) - channel = self._request_rocket_chat("get", url_search) + url_path = "channels.info" + payload = {"roomName": room_name} + channel = self._request_rocket_chat("get", url_path, payload=payload) if "channel" in channel and channel["channel"]["t"] == "c": channel_id = channel["channel"]["_id"] @@ -134,16 +135,18 @@ def search_rocket_chat_group(self, room_name): """ This method gets a group with a specific name and returns a json with group's info """ - url_path = "{}?{}={}".format("groups.info", "roomName", room_name) - return self._request_rocket_chat("get", url_path) + url_path = "groups.info" + payload = {"roomName": room_name} + return self._request_rocket_chat("get", url_path, payload=payload) def search_rocket_chat_user(self, username): """ This method allows to get a user from RocketChat data base """ - url_path = "{}?{}={}".format("users.info", "username", username) + url_path = "users.info" + payload = {"username": username} - return self._request_rocket_chat("get", url_path) + return self._request_rocket_chat("get", url_path, payload=payload) def set_avatar(self, username, image_url): """ @@ -154,7 +157,7 @@ def set_avatar(self, username, image_url): response = self._request_rocket_chat("post", url_path, data) LOG.info("Method set avatar: %s with this data: %s", response, data) - def set_description(self, group_id, description): + def set_group_description(self, group_id, description): """ This method allows to set a description's group """ @@ -169,7 +172,7 @@ def set_description(self, group_id, description): LOG.info("Method Set Description %s with this data: %s", response, data) - def set_topic(self, group_id, topic): + def set_group_topic(self, group_id, topic): """ This method allows to set a topic's group """ diff --git a/rocketc/api_teams.py b/rocketc/api_teams.py index 3fc2854c..277bca12 100644 --- a/rocketc/api_teams.py +++ b/rocketc/api_teams.py @@ -29,16 +29,13 @@ def __init__(self, username, password, client_id, client_secret, # pylint: disa def _get_token(self, server_url): """This method get the Authorization token""" - client_id = self.client_id - client_secret = self.client_secret - oauth = OAuth2Session( - client=LegacyApplicationClient(client_id=client_id)) + client=LegacyApplicationClient(client_id=self.client_id)) token_url = "/".join([server_url, "oauth2/access_token/"]) token = oauth.fetch_token(token_url=token_url, - client_id=client_id, - client_secret=client_secret, + client_id=self.client_id, + client_secret=self.client_secret, username=self.username, password=self.password) @@ -46,17 +43,17 @@ def _get_token(self, server_url): return token['access_token'] - def _call_api_get(self, url_path): + def _call_api_get(self, url_path, payload): """This method return the response""" url = "/".join([self.server_url, self.API_PATH, url_path]) - return requests.get(url, headers=self.headers) + return requests.get(url, headers=self.headers, params=payload) def get_user_team(self, course_id, username): """Get the user's team""" course_id = course_id.to_deprecated_string().replace("+", "%2B") - url_path = "teams/?course_id={}&username={}".format( - course_id, username) - team_request = self._call_api_get(url_path) + url_path = "teams" + payload = {"course_id": course_id, "username": username} + team_request = self._call_api_get(url_path, payload) if team_request.status_code == 200: return team_request.json()["results"] diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index dcd71b0f..d5334abe 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -57,7 +57,7 @@ class RocketChatXBlock(XBlock, XBlockWithSettingsMixin, StudioEditableXBlockMixi help="This is the flag for the initial channel", ) - channel = String( + selected_view = String( display_name="Select Channel", default="Main View", scope=Scope.content, @@ -67,6 +67,8 @@ class RocketChatXBlock(XBlock, XBlockWithSettingsMixin, StudioEditableXBlockMixi team_channel = "" + VIEWS = ["Main View", "Team Discussion", "Specific Channel"] + # Possible editable fields editable_fields = ('channel', 'default_channel') @@ -103,7 +105,7 @@ def student_view(self, context=None): def author_view(self, context=None): """ Returns author view fragment on Studio """ # pylint: disable=unused-argument - self.api_rocket_chat.private_channel("general") + self.api_rocket_chat.convert_to_private_channel("general") frag = Fragment(u"Studio Runtime RocketChatXBlock") frag.add_css(self.resource_string("static/css/rocketc.css")) frag.add_javascript(self.resource_string("static/js/src/rocketc.js")) @@ -182,7 +184,7 @@ def user_data(self): user_data["username"] = user.opt_attrs['edx-platform.username'] user_data["anonymous_student_id"] = runtime.anonymous_student_id - if self.channel == "Team Discussion": + if self.selected_view == "Team Discussion": user_data["default_group"] = self.team_channel else: user_data["default_group"] = self.default_channel @@ -201,8 +203,9 @@ def channels_enabled(self): This method returns a list with the channel options """ if self._teams_is_enabled(): - return ["Main View", "Team Discussion", "Specific Channel"] - return ["Main View", "Specific Channel"] + return self.VIEWS + self.VIEWS.remove(self.VIEWS[1]) + return self.VIEWS def get_groups(self): """ @@ -223,11 +226,11 @@ def init(self): response = response['data'] user_id = response['userId'] - self._join_groups(user_id, user_data) + self._join_user_to_groups(user_id, user_data) self._update_user(user_id, user_data) if user_data["role"] == "instructor" and self.rocket_chat_role == "user": - self.api_rocket_chat.change_role(user_id, "bot") + self.api_rocket_chat.change_user_role(user_id, "bot") return response else: return response['errorType'] @@ -256,7 +259,7 @@ def login(self, user_data): return data - def _add_to_course_group(self, group_name, user_id): + def _add_user_to_course_group(self, group_name, user_id): """ This method add the user to the default course channel """ @@ -264,25 +267,25 @@ def _add_to_course_group(self, group_name, user_id): rocket_chat_group = api.search_rocket_chat_group(group_name) if rocket_chat_group['success']: - api.add_to_group(user_id, rocket_chat_group['group']['_id']) + api.add_user_to_group(user_id, rocket_chat_group['group']['_id']) else: rocket_chat_group = api.create_group(group_name, self.user_data["username"]) self.group = api.search_rocket_chat_group( # pylint: disable=attribute-defined-outside-init group_name) - def _add_to_default_group(self, group_name, user_id): + def _add_user_to_default_group(self, group_name, user_id): """ """ api = self.api_rocket_chat group_info = api.search_rocket_chat_group(group_name) if group_info["success"]: - api.add_to_group(user_id, group_info['group']['_id']) + api.add_user_to_group(user_id, group_info['group']['_id']) return True return False - def _add_to_team_group(self, user_id, username, course_id): + def _add_user_to_team_group(self, user_id, username, course_id): """ Add the user to team's group in rocketChat """ @@ -297,7 +300,7 @@ def _add_to_team_group(self, user_id, username, course_id): self.team_channel = group_name if group_info["success"]: - response = api.add_to_group(user_id, group_info['group']['_id']) + response = api.add_user_to_group(user_id, group_info['group']['_id']) LOG.info("Add to team group response: %s", response) return response["success"] @@ -326,23 +329,22 @@ def _get_team(self, username, course_id): return team[0] return None - def _join_groups(self, user_id, user_data): + def _join_user_to_groups(self, user_id, user_data): """ This methodd add the user to the diferent channels """ default_channel = self.default_channel - channel = self.channel - if channel == "Team Discussion" and self._teams_is_enabled(): - self.ui_is_block = self._add_to_team_group( + if self.selected_view == "Team Discussion" and self._teams_is_enabled(): + self.ui_is_block = self._add_user_to_team_group( user_id, user_data["username"], user_data["course_id"]) - elif channel == "Specific Channel": - self.ui_is_block = self._add_to_default_group(default_channel, user_id) + elif self.selected_view == "Specific Channel": + self.ui_is_block = self._add_user_to_default_group(default_channel, user_id) else: self.ui_is_block = False - self._add_to_course_group(user_data["course"], user_id) + self._add_user_to_course_group(user_data["course"], user_id) def _teams_is_enabled(self): """ @@ -364,6 +366,7 @@ def _teams_is_enabled(self): def _update_user(self, user_id, user_data): """ + This method updates the email and photo's profile """ api = self.api_rocket_chat if user_data["email"] != self.email: @@ -411,8 +414,8 @@ def create_group(self, data, suffix=""): if "group" in group: group_id = group["group"]["_id"] - api.set_description(group_id, description) - api.set_topic(group_id, topic) + api.set_group_description(group_id, description) + api.set_group_topic(group_id, topic) LOG.info("Method Public Create Group: %s", group) return group diff --git a/rocketc/tests/test_rocketChat.py b/rocketc/tests/test_rocketChat.py index 3bbfc47b..ed402648 100644 --- a/rocketc/tests/test_rocketChat.py +++ b/rocketc/tests/test_rocketChat.py @@ -29,27 +29,27 @@ def setUp(self): self.module_patcher.start() @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') - def test_add_to_course_group(self, mock_api_rocket): + def test_add_user_to_course_group(self, mock_api_rocket): """Test for the add course group method""" group_name = "test_group" user_id = "test_user_id" data = {'success': True, 'group': {'_id': "test_group_id"}} mock_api_rocket.search_rocket_chat_group.return_value = data - self.block._add_to_course_group(group_name, user_id) - mock_api_rocket.add_to_group.assert_called_with( + self.block._add_user_to_course_group(group_name, user_id) + mock_api_rocket.add_user_to_group.assert_called_with( user_id, data['group']['_id']) with patch('rocketc.rocketc.RocketChatXBlock.user_data', new_callable=PropertyMock) as mock_user: mock_user.return_value = {"username": "test_user_name"} data['success'] = False mock_api_rocket.search_rocket_chat_group.return_value = data - self.block._add_to_course_group(group_name, user_id) + self.block._add_user_to_course_group(group_name, user_id) mock_api_rocket.create_group.assert_called_with( group_name, "test_user_name") @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') - def test_add_to_default_group(self, mock_api_rocket): + def test_add_user_to_default_group(self, mock_api_rocket): """ Test the method add to default group """ @@ -58,17 +58,17 @@ def test_add_to_default_group(self, mock_api_rocket): mock_api_rocket.search_rocket_chat_group.return_value = {"success": True, "group": {"_id": "1234"}} - self.assertTrue(self.block._add_to_default_group(group_name, user_id)) - mock_api_rocket.add_to_group.assert_called_with(user_id, "1234") + self.assertTrue(self.block._add_user_to_default_group(group_name, user_id)) + mock_api_rocket.add_user_to_group.assert_called_with(user_id, "1234") mock_api_rocket.search_rocket_chat_group.return_value = {"success": False} - self.assertFalse(self.block._add_to_default_group(group_name, user_id)) - mock_api_rocket.add_to_group.assert_called_with(user_id, "1234") + self.assertFalse(self.block._add_user_to_default_group(group_name, user_id)) + mock_api_rocket.add_user_to_group.assert_called_with(user_id, "1234") @patch('rocketc.rocketc.RocketChatXBlock._get_team') @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') - def test_add_to_team_group(self, mock_api_rocket, mock_get_team): + def test_add_user_to_team_group(self, mock_api_rocket, mock_get_team): """ test method add to team group """ @@ -78,18 +78,18 @@ def test_add_to_team_group(self, mock_api_rocket, mock_get_team): mock_get_team.return_value = None - self.assertFalse(self.block._add_to_team_group(user_id, username, course_id)) + self.assertFalse(self.block._add_user_to_team_group(user_id, username, course_id)) mock_get_team.return_value = {"name": "name", "topic_id": "test"} mock_api_rocket.search_rocket_chat_group.return_value = {"success": True, "group": {"_id": "1234"}} - self.block._add_to_team_group(user_id, username, course_id) + self.block._add_user_to_team_group(user_id, username, course_id) self.assertEqual(self.block.team_channel, "Team-test-name") - mock_api_rocket.add_to_group.assert_called_with(user_id, "1234") + mock_api_rocket.add_user_to_group.assert_called_with(user_id, "1234") mock_api_rocket.search_rocket_chat_group.return_value = {"success": False} - self.block._add_to_team_group(user_id, username, course_id) + self.block._add_user_to_team_group(user_id, username, course_id) mock_api_rocket.create_group.assert_called_with("Team-test-name", username) @@ -136,9 +136,9 @@ def test_create_group(self, mock_api_rocket): @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') @patch('rocketc.rocketc.RocketChatXBlock._update_user') - @patch('rocketc.rocketc.RocketChatXBlock._join_groups') + @patch('rocketc.rocketc.RocketChatXBlock._join_user_to_groups') @patch('rocketc.rocketc.RocketChatXBlock.login') - def test_init(self, mock_login, mock_join_groups, mock_update_user, mock_api_rocket): + def test_init(self, mock_login, mock_join_user, mock_update_user, mock_api_rocket): """ Test the method to initialize the xblock """ @@ -152,9 +152,9 @@ def test_init(self, mock_login, mock_join_groups, mock_update_user, mock_api_roc self.block.rocket_chat_role = "user" self.assertEqual(self.block.init(), data) - mock_join_groups.assert_called_with(user_id, user_data) + mock_join_user.assert_called_with(user_id, user_data) mock_update_user.assert_called_with(user_id, user_data) - mock_api_rocket.change_role.assert_called_with(user_id, "bot") + mock_api_rocket.change_user_role.assert_called_with(user_id, "bot") mock_login.return_value = {"success": False, "errorType": "test_error"} @@ -206,7 +206,7 @@ def test_get_team(self, mock_teams): mock_teams.return_value.get_user_team.return_value = [] self.assertIsNone(self.block._get_team(username, course_id)) - def test_join_groups(self): + def test_join_user_to_groups(self): """ Test the method join groups """ @@ -215,23 +215,23 @@ def test_join_groups(self): user_data = {"course_id": "test_course_id", "username": "test_user_name", "course": "test_course"} self.block.default_channel = "test_default_channel" - self.block.channel = "Team Discussion" + self.block.selected_view = "Team Discussion" with patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled', return_value=True): - with patch('rocketc.rocketc.RocketChatXBlock._add_to_team_group', return_value=True): - self.block._join_groups(user_id, user_data) + with patch('rocketc.rocketc.RocketChatXBlock._add_user_to_team_group', return_value=True): + self.block._join_user_to_groups(user_id, user_data) self.assertTrue(self.block.ui_is_block) - self.block.channel = "Specific Channel" + self.block.selected_view = "Specific Channel" - with patch('rocketc.rocketc.RocketChatXBlock._add_to_default_group', return_value=True): - self.block._join_groups(user_id, user_data) + with patch('rocketc.rocketc.RocketChatXBlock._add_user_to_default_group', return_value=True): + self.block._join_user_to_groups(user_id, user_data) self.assertTrue(self.block.ui_is_block) - self.block.channel = "" + self.block.selected_view = "" - with patch('rocketc.rocketc.RocketChatXBlock._add_to_course_group'): - self.block._join_groups(user_id, user_data) + with patch('rocketc.rocketc.RocketChatXBlock._add_user_to_course_group'): + self.block._join_user_to_groups(user_id, user_data) self.assertFalse(self.block.ui_is_block) @patch('rocketc.rocketc.RocketChatXBlock._user_image_url') @@ -283,7 +283,7 @@ def setUp(self): self.api = ApiRocketChat(user, password, server_url) @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') - def test_add_to_group(self, mock_request): + def test_add_user_to_group(self, mock_request): """Test for the add group method""" method = "post" success = {'success': True} @@ -296,12 +296,12 @@ def test_add_to_group(self, mock_request): data = {"roomId": room_id, "userId": user_id} - response = self.api.add_to_group(user_id, room_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_role(self, mock_request): + def test_change_user_role(self, mock_request): """Test for chage role method""" method = "post" success = {'success': True} @@ -314,7 +314,7 @@ def test_change_role(self, mock_request): data = {"userId": user_id, "data": {"roles": [role]}} - self.api.change_role(user_id, 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') @@ -417,7 +417,7 @@ def test_login(self, mock_request): self.assertEqual(self.api.headers["X-User-Id"], "test_id") @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') - def test_private_channel(self, mock_request): + def test_convert_to_private_channel(self, mock_request): """Test for private channel method""" method = "post" room_name = "test_room_name" @@ -425,7 +425,7 @@ def test_private_channel(self, mock_request): data = {"roomId": "1234", "type": "p"} mock_request.return_value = {"channel": {"t": "c", "_id": "1234"}} - self.api.private_channel(room_name) + self.api.convert_to_private_channel(room_name) mock_request.assert_called_with(method, url_path, data) @@ -464,11 +464,12 @@ def test_search_rocket_chat_group(self, mock_request): success = {'success': True} room_name = "test_room_name" mock_request.return_value = success - url_path = "{}?{}={}".format("groups.info", "roomName", room_name) + 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) + mock_request.assert_called_with(method, url_path, payload=payload) self.assertTrue(response['success']) @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') @@ -478,11 +479,12 @@ def test_search_rocket_chat_user(self, mock_request): success = {'success': True} username = "test_user_name" mock_request.return_value = success - url_path = "{}?{}={}".format("users.info", "username", username) + url_path = "users.info" + payload = {"username": username} response = self.api.search_rocket_chat_user(username) - mock_request.assert_called_with(method, url_path) + mock_request.assert_called_with(method, url_path, payload=payload) self.assertTrue(response['success']) @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') @@ -499,14 +501,14 @@ def test_set_avatar(self, mock_request): mock_request.assert_called_with(method, url_path, data) @patch('rocketc.api_rocket_chat.ApiRocketChat._request_rocket_chat') - def test_set_description(self, mock_request): + 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_description(group_id, description) + return_none = self.api.set_group_description(group_id, description) self.assertIsNone(return_none) @@ -516,18 +518,18 @@ def test_set_description(self, mock_request): data = {"roomId": group_id, "description": description} method = "post" - self.api.set_description(group_id, description) + 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_topic(self, mock_request): + 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_topic(group_id, topic) + return_none = self.api.set_group_topic(group_id, topic) self.assertIsNone(return_none) @@ -537,7 +539,7 @@ def test_set_topic(self, mock_request): data = {"roomId": group_id, "topic": topic} method = "post" - self.api.set_topic(group_id, topic) + 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') From 060c7df5e2f14d38493e80c9d5259f8e337c22f1 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Mon, 30 Apr 2018 17:36:03 -0500 Subject: [PATCH 5/8] Add test api_teams --- rocketc/api_teams.py | 2 +- rocketc/rocketc.py | 6 +-- rocketc/tests/test_rocketChat.py | 66 +++++++++++++++++++++++++++----- 3 files changed, 61 insertions(+), 13 deletions(-) diff --git a/rocketc/api_teams.py b/rocketc/api_teams.py index 277bca12..4314b460 100644 --- a/rocketc/api_teams.py +++ b/rocketc/api_teams.py @@ -43,7 +43,7 @@ def _get_token(self, server_url): return token['access_token'] - def _call_api_get(self, url_path, payload): + def _call_api_get(self, url_path, payload=None): """This method return the response""" url = "/".join([self.server_url, self.API_PATH, url_path]) return requests.get(url, headers=self.headers, params=payload) diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index d5334abe..2ae936e4 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -184,7 +184,7 @@ def user_data(self): user_data["username"] = user.opt_attrs['edx-platform.username'] user_data["anonymous_student_id"] = runtime.anonymous_student_id - if self.selected_view == "Team Discussion": + if self.selected_view == VIEWS[1]: user_data["default_group"] = self.team_channel else: user_data["default_group"] = self.default_channel @@ -335,11 +335,11 @@ def _join_user_to_groups(self, user_id, user_data): """ default_channel = self.default_channel - if self.selected_view == "Team Discussion" and self._teams_is_enabled(): + if self.selected_view == VIEWS[1] and self._teams_is_enabled(): self.ui_is_block = self._add_user_to_team_group( user_id, user_data["username"], user_data["course_id"]) - elif self.selected_view == "Specific Channel": + elif self.selected_view == VIEWS[2]: self.ui_is_block = self._add_user_to_default_group(default_channel, user_id) else: diff --git a/rocketc/tests/test_rocketChat.py b/rocketc/tests/test_rocketChat.py index ed402648..796e7dd7 100644 --- a/rocketc/tests/test_rocketChat.py +++ b/rocketc/tests/test_rocketChat.py @@ -6,6 +6,7 @@ from mock import MagicMock, patch, PropertyMock from rocketc.rocketc import RocketChatXBlock from rocketc.api_rocket_chat import ApiRocketChat +from rocketc.api_teams import ApiTeams class TestRocketChat(unittest.TestCase): @@ -56,14 +57,18 @@ def test_add_user_to_default_group(self, mock_api_rocket): group_name = "test_group_name" user_id = "test_user_id" - mock_api_rocket.search_rocket_chat_group.return_value = {"success": True, "group": {"_id": "1234"}} + mock_api_rocket.search_rocket_chat_group.return_value = { + "success": True, "group": {"_id": "1234"}} - self.assertTrue(self.block._add_user_to_default_group(group_name, user_id)) + self.assertTrue( + self.block._add_user_to_default_group(group_name, user_id)) mock_api_rocket.add_user_to_group.assert_called_with(user_id, "1234") - mock_api_rocket.search_rocket_chat_group.return_value = {"success": False} + mock_api_rocket.search_rocket_chat_group.return_value = { + "success": False} - self.assertFalse(self.block._add_user_to_default_group(group_name, user_id)) + self.assertFalse( + self.block._add_user_to_default_group(group_name, user_id)) mock_api_rocket.add_user_to_group.assert_called_with(user_id, "1234") @patch('rocketc.rocketc.RocketChatXBlock._get_team') @@ -78,20 +83,24 @@ def test_add_user_to_team_group(self, mock_api_rocket, mock_get_team): mock_get_team.return_value = None - self.assertFalse(self.block._add_user_to_team_group(user_id, username, course_id)) + self.assertFalse(self.block._add_user_to_team_group( + user_id, username, course_id)) mock_get_team.return_value = {"name": "name", "topic_id": "test"} - mock_api_rocket.search_rocket_chat_group.return_value = {"success": True, "group": {"_id": "1234"}} + mock_api_rocket.search_rocket_chat_group.return_value = { + "success": True, "group": {"_id": "1234"}} self.block._add_user_to_team_group(user_id, username, course_id) self.assertEqual(self.block.team_channel, "Team-test-name") mock_api_rocket.add_user_to_group.assert_called_with(user_id, "1234") - mock_api_rocket.search_rocket_chat_group.return_value = {"success": False} + mock_api_rocket.search_rocket_chat_group.return_value = { + "success": False} self.block._add_user_to_team_group(user_id, username, course_id) - mock_api_rocket.create_group.assert_called_with("Team-test-name", username) + mock_api_rocket.create_group.assert_called_with( + "Team-test-name", username) @patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled') def test_channels_enabled(self, mock_team): @@ -156,7 +165,8 @@ def test_init(self, mock_login, mock_join_user, mock_update_user, mock_api_rocke mock_update_user.assert_called_with(user_id, user_data) mock_api_rocket.change_user_role.assert_called_with(user_id, "bot") - mock_login.return_value = {"success": False, "errorType": "test_error"} + mock_login.return_value = { + "success": False, "errorType": "test_error"} self.assertEqual(self.block.init(), "test_error") @@ -560,3 +570,41 @@ def test_update_user(self, mock_request): self.assertEqual(new_email, email) mock_request.assert_called_with(method, url_path, data) + + +class TestApiTeams(unittest.TestCase): + """""" + + def setUp(self): + """Set up general variables""" + username = "test_user_name" + password = "test_password" + server_url = "http://test_server" + client_secret = "test_client_secret" + client_id = "test_client_id" + + with patch('rocketc.api_teams.ApiTeams._get_token') as mock_token: + mock_token.return_value = "test_token" + self.api = ApiTeams(username, password, client_id, client_secret, server_url) + + @patch('rocketc.api_teams.requests.get') + def test_call_api_get(self, mock_request): + """Test for the method call api get""" + url_path = "test_path" + payload = {"data": "data_test"} + url = "/".join([self.api.server_url, self.api.API_PATH, url_path]) + + self.api._call_api_get(url_path, payload) + mock_request.assert_called_with(url, headers=self.api.headers, params=payload) + + # def test_get_user_team(self, course_id, username): + # """Get the user's team""" + # course_id = MagicMock() + # username = "test_user_name" + # url_path = "teams" + # payload = {"course_id": course_id, "username": username} + # team_request = self._call_api_get(url_path, payload) + + # if team_request.status_code == 200: + # return team_request.json()["results"] + # return team_request.json() From 35127af6da512df5718ca32658bc712c1f9a0b56 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Wed, 2 May 2018 12:15:29 -0500 Subject: [PATCH 6/8] Fixing and adding test --- rocketc/rocketc.py | 22 +++++---- rocketc/tests/test_rocketChat.py | 82 ++++++++++++++++++++++++++------ 2 files changed, 81 insertions(+), 23 deletions(-) diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index 2ae936e4..b0621bbc 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -184,7 +184,7 @@ def user_data(self): user_data["username"] = user.opt_attrs['edx-platform.username'] user_data["anonymous_student_id"] = runtime.anonymous_student_id - if self.selected_view == VIEWS[1]: + if self.selected_view == self.VIEWS[1]: user_data["default_group"] = self.team_channel else: user_data["default_group"] = self.default_channel @@ -204,14 +204,19 @@ def channels_enabled(self): """ if self._teams_is_enabled(): return self.VIEWS - self.VIEWS.remove(self.VIEWS[1]) - return self.VIEWS + view = list(self.VIEWS) + view.remove(self.VIEWS[1]) + return view def get_groups(self): """ This method lists the existing groups """ - return self.api_rocket_chat.get_groups() + groups = self.api_rocket_chat.get_groups() + for group in groups: + if group.startswith("Team"): + groups.remove(group) + return groups def init(self): """ @@ -229,8 +234,8 @@ def init(self): self._join_user_to_groups(user_id, user_data) self._update_user(user_id, user_data) - if user_data["role"] == "instructor" and self.rocket_chat_role == "user": - self.api_rocket_chat.change_user_role(user_id, "bot") + if user_data["role"] == "instructor" and self.rocket_chat_role != "leader": + self.api_rocket_chat.change_user_role(user_id, "leader") return response else: return response['errorType'] @@ -334,12 +339,13 @@ def _join_user_to_groups(self, user_id, user_data): This methodd add the user to the diferent channels """ default_channel = self.default_channel + print self.VIEWS - if self.selected_view == VIEWS[1] and self._teams_is_enabled(): + if self.selected_view == self.VIEWS[1] and self._teams_is_enabled(): self.ui_is_block = self._add_user_to_team_group( user_id, user_data["username"], user_data["course_id"]) - elif self.selected_view == VIEWS[2]: + elif self.selected_view == self.VIEWS[2]: self.ui_is_block = self._add_user_to_default_group(default_channel, user_id) else: diff --git a/rocketc/tests/test_rocketChat.py b/rocketc/tests/test_rocketChat.py index 796e7dd7..4e2cd245 100644 --- a/rocketc/tests/test_rocketChat.py +++ b/rocketc/tests/test_rocketChat.py @@ -102,6 +102,14 @@ def test_add_user_to_team_group(self, mock_api_rocket, mock_get_team): mock_api_rocket.create_group.assert_called_with( "Team-test-name", username) + def test_api_rocket_chat(self): + """ + Test api rocket chat + """ + with patch('rocketc.rocketc.ApiRocketChat._login'): + with patch('rocketc.rocketc.RocketChatXBlock.xblock_settings'): + self.assertIsInstance(self.block.api_rocket_chat, ApiRocketChat) + @patch('rocketc.rocketc.RocketChatXBlock._teams_is_enabled') def test_channels_enabled(self, mock_team): """ @@ -143,6 +151,15 @@ def test_create_group(self, mock_api_rocket): self.assertEqual(json.loads(result.body), { "success": True, "group": {"_id": "1234"}}) + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') + def test_get_groups(self, mock_api_rocket): + """ + Test get_groups + """ + mock_api_rocket.get_groups.return_value = ["Team-group", "group1", "group2", "Team-test-group"] + groups = self.block.get_groups() + self.assertEqual(groups, ["group1", "group2"]) + @patch('rocketc.rocketc.RocketChatXBlock.api_rocket_chat') @patch('rocketc.rocketc.RocketChatXBlock._update_user') @patch('rocketc.rocketc.RocketChatXBlock._join_user_to_groups') @@ -163,7 +180,7 @@ def test_init(self, mock_login, mock_join_user, mock_update_user, mock_api_rocke self.assertEqual(self.block.init(), data) mock_join_user.assert_called_with(user_id, user_data) mock_update_user.assert_called_with(user_id, user_data) - mock_api_rocket.change_user_role.assert_called_with(user_id, "bot") + mock_api_rocket.change_user_role.assert_called_with(user_id, "leader") mock_login.return_value = { "success": False, "errorType": "test_error"} @@ -259,6 +276,25 @@ def test_update_user(self, mock_api_rocket, mock_url): mock_api_rocket.set_avatar.assert_called_with( user_data["username"], "test_url") + def test_user_data(self): + + team_channel = "test-team-channel" + default_channel = "test-default-channel" + mock_runtime = MagicMock() + mock_runtime.course_id._to_string.return_value = "test_course_id" + self.block.xmodule_runtime = mock_runtime + self.block.selected_view = "Team Discussion" + self.block.team_channel = team_channel + self.block.default_channel = default_channel + + data = self.block.user_data + self.assertEqual(data["default_group"], team_channel) + + self.block.selected_view = "Another Discussion" + + data = self.block.user_data + self.assertEqual(data["default_group"], default_channel) + @override_settings(LMS_ROOT_URL="http://127.0.0.1/") @patch('rocketc.rocketc.User') @patch('rocketc.rocketc.RocketChatXBlock.user_data') @@ -585,7 +621,8 @@ def setUp(self): with patch('rocketc.api_teams.ApiTeams._get_token') as mock_token: mock_token.return_value = "test_token" - self.api = ApiTeams(username, password, client_id, client_secret, server_url) + self.api = ApiTeams(username, password, client_id, + client_secret, server_url) @patch('rocketc.api_teams.requests.get') def test_call_api_get(self, mock_request): @@ -595,16 +632,31 @@ def test_call_api_get(self, mock_request): url = "/".join([self.api.server_url, self.api.API_PATH, url_path]) self.api._call_api_get(url_path, payload) - mock_request.assert_called_with(url, headers=self.api.headers, params=payload) - - # def test_get_user_team(self, course_id, username): - # """Get the user's team""" - # course_id = MagicMock() - # username = "test_user_name" - # url_path = "teams" - # payload = {"course_id": course_id, "username": username} - # team_request = self._call_api_get(url_path, payload) - - # if team_request.status_code == 200: - # return team_request.json()["results"] - # return team_request.json() + mock_request.assert_called_with( + url, headers=self.api.headers, params=payload) + + @patch('rocketc.api_teams.OAuth2Session') + def test_get_token(self, mock_oauth): + """test get_token """ + server_url = "test_server" + token_url = "/".join([server_url, "oauth2/access_token/"]) + self.api._get_token(server_url) + mock_oauth.return_value.fetch_token.assert_called_with(token_url=token_url, + client_id=self.api.client_id, + client_secret=self.api.client_secret, + username=self.api.username, + password=self.api.password) + + @patch('rocketc.api_teams.ApiTeams._call_api_get') + def test_get_user_team(self, mock_call): + """Get the user's team""" + course_id = MagicMock() + course_id.to_deprecated_string.return_value = "test_course_id" + username = "test_user_name" + url_path = "teams" + payload = {"course_id": "test_course_id", "username": username} + + mock_call.return_value = MagicMock(status_code=200) + + self.api.get_user_team(course_id, username) + mock_call.assert_called_with(url_path, payload) From d8ddf2d9eb91083e1544137a25eff6e167ad007c Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Wed, 2 May 2018 15:16:04 -0500 Subject: [PATCH 7/8] Fixing api_teams calls and javascript --- rocketc/api_teams.py | 2 +- rocketc/rocketc.py | 13 +++++-------- rocketc/static/js/src/studio_view.js | 4 ++-- rocketc/tests/test_rocketChat.py | 2 +- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/rocketc/api_teams.py b/rocketc/api_teams.py index 4314b460..d1c5639b 100644 --- a/rocketc/api_teams.py +++ b/rocketc/api_teams.py @@ -50,7 +50,7 @@ def _call_api_get(self, url_path, payload=None): def get_user_team(self, course_id, username): """Get the user's team""" - course_id = course_id.to_deprecated_string().replace("+", "%2B") + course_id = course_id.to_deprecated_string() url_path = "teams" payload = {"course_id": course_id, "username": username} team_request = self._call_api_get(url_path, payload) diff --git a/rocketc/rocketc.py b/rocketc/rocketc.py index b0621bbc..4807aa5b 100644 --- a/rocketc/rocketc.py +++ b/rocketc/rocketc.py @@ -36,7 +36,7 @@ class RocketChatXBlock(XBlock, XBlockWithSettingsMixin, StudioEditableXBlockMixi default="Rocket Chat" ) email = String( - default="", scope=Scope.user_info, + default="", scope=Scope.user_state, help="Email in rocketChat", ) rocket_chat_role = String( @@ -70,7 +70,7 @@ class RocketChatXBlock(XBlock, XBlockWithSettingsMixin, StudioEditableXBlockMixi VIEWS = ["Main View", "Team Discussion", "Specific Channel"] # Possible editable fields - editable_fields = ('channel', 'default_channel') + editable_fields = ('selected_view', 'default_channel') def resource_string(self, path): """Handy helper for getting resources from our kit.""" @@ -213,9 +213,7 @@ def get_groups(self): This method lists the existing groups """ groups = self.api_rocket_chat.get_groups() - for group in groups: - if group.startswith("Team"): - groups.remove(group) + groups = [group for group in groups if not group.startswith("Team")] return groups def init(self): @@ -234,8 +232,8 @@ def init(self): self._join_user_to_groups(user_id, user_data) self._update_user(user_id, user_data) - if user_data["role"] == "instructor" and self.rocket_chat_role != "leader": - self.api_rocket_chat.change_user_role(user_id, "leader") + if user_data["role"] == "instructor" and self.rocket_chat_role != "bot": + self.api_rocket_chat.change_user_role(user_id, "bot") return response else: return response['errorType'] @@ -339,7 +337,6 @@ def _join_user_to_groups(self, user_id, user_data): This methodd add the user to the diferent channels """ default_channel = self.default_channel - print self.VIEWS if self.selected_view == self.VIEWS[1] and self._teams_is_enabled(): self.ui_is_block = self._add_user_to_team_group( diff --git a/rocketc/static/js/src/studio_view.js b/rocketc/static/js/src/studio_view.js index 889ab8e7..83464a45 100644 --- a/rocketc/static/js/src/studio_view.js +++ b/rocketc/static/js/src/studio_view.js @@ -47,7 +47,7 @@ function StudioViewEdit(runtime, element) { $("#select-default").attr("class", "button"); }); - $("#xb-field-edit-channel").on("change", function() { + $("#xb-field-edit-selected_view").on("change", function() { if(this.value === "Specific Channel"){ $("#xb-field-edit-default_channel").prop("disabled", false); @@ -58,7 +58,7 @@ function StudioViewEdit(runtime, element) { $(function ($) { - if( $( "#xb-field-edit-channel" ).val() === "Specific Channel"){ + if( $( "#xb-field-edit-selected_view" ).val() === "Specific Channel"){ $("#xb-field-edit-default_channel").prop("disabled", false); }else{ $("#xb-field-edit-default_channel").prop("disabled", true); diff --git a/rocketc/tests/test_rocketChat.py b/rocketc/tests/test_rocketChat.py index 4e2cd245..79867f23 100644 --- a/rocketc/tests/test_rocketChat.py +++ b/rocketc/tests/test_rocketChat.py @@ -180,7 +180,7 @@ def test_init(self, mock_login, mock_join_user, mock_update_user, mock_api_rocke self.assertEqual(self.block.init(), data) mock_join_user.assert_called_with(user_id, user_data) mock_update_user.assert_called_with(user_id, user_data) - mock_api_rocket.change_user_role.assert_called_with(user_id, "leader") + mock_api_rocket.change_user_role.assert_called_with(user_id, "bot") mock_login.return_value = { "success": False, "errorType": "test_error"} From 306698913fea9aad0c7dee69eca95ffb24580bc4 Mon Sep 17 00:00:00 2001 From: andrey-canon Date: Thu, 10 May 2018 14:55:09 -0500 Subject: [PATCH 8/8] =?UTF-8?q?Bump=20version:=200.2.5=20=E2=86=92=200.2.6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- setup.cfg | 2 +- setup.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/setup.cfg b/setup.cfg index 23987634..5352146d 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.2.5 +current_version = 0.2.6 commit = True tag = True diff --git a/setup.py b/setup.py index 3e75a08f..1a1acc4d 100644 --- a/setup.py +++ b/setup.py @@ -4,7 +4,7 @@ from setuptools import setup -__version__ = '0.2.5' +__version__ = '0.2.6' def package_data(pkg, roots):