def test_build_mail_context_with_request(self): raw_content_dict = {"var1": 'vvv1', 'var2': 'vvv2'} en_ctx = Context(raw_content_dict) en_ctx['LANGUAGE_CODE'] = 'en' en_ctx['LANGUAGES'] = settings.LANGUAGES zh_ctx = Context(raw_content_dict) zh_ctx['LANGUAGE_CODE'] = 'zh-Hant' zh_ctx['LANGUAGES'] = settings.LANGUAGES en_request = request.HttpRequest() en_request.META['HTTP_ACCEPT_LANGUAGE'] = 'en' zh_request = request.HttpRequest() zh_request.META['HTTP_ACCEPT_LANGUAGE'] = 'zh-Hant' w_request = request.HttpRequest() w_request.META['HTTP_ACCEPT_LANGUAGE'] = 'wrong' test_cases = [ (en_request, en_ctx), (zh_request, zh_ctx), (w_request, en_ctx), ] for r, exp in test_cases: got = utils.build_mail_context_with_request(raw_content_dict, r) self.assertEqual( exp, got, "\n\nParam: {}\n\nExp: {}\n\nGot: {}\n\n".format((raw_content_dict, r), exp, got))
def test_that_middleware_does_nothing_to_html_requests(self): resp = response.HttpResponse(mimetype='text/html', content='<html></html>') mware = middleware.AngularJsonVulnerabilityMiddleware() mware.process_response(request.HttpRequest(), resp) self.assertEqual(resp.content, '<html></html>')
def test_that_middleware_adds_prefix_to_valid_json_requests(self): resp = response.HttpResponse(mimetype='application/json', content='[1, 2, 3]') mware = middleware.AngularJsonVulnerabilityMiddleware() mware.process_response(request.HttpRequest(), resp) self.assertEqual(resp.content, mware.CONTENT_PREFIX + '[1, 2, 3]')
def test_that_middleware_does_nothing_to_js_requests(self): resp = response.HttpResponse(mimetype='text/javascript', content='var blah = [];') mware = middleware.AngularJsonVulnerabilityMiddleware() mware.process_response(request.HttpRequest(), resp) self.assertEqual(resp.content, 'var blah = [];')
def test_that_middleware_does_nothing_to_invalid_json_requests(self): resp = response.HttpResponse(mimetype='application/json', content='[1, 2, 3]', status=400) mware = middleware.AngularJsonVulnerabilityMiddleware() mware.process_response(request.HttpRequest(), resp) self.assertEqual(resp.content, '[1, 2, 3]')
def test_zk_info(self): """ 测试获取zk信息 :return: """ req_data = { "username": self.username, "password": self.password, "zkPath": self.zk_path } # 手动构建request req = request.HttpRequest() req.method = 'POST' for key, value in req_data.items(): req.POST.setdefault(key, value) resp = zookeeper(req) zk_info = json.loads(resp.content) self.assertEqual(zk_info["status"], 0), "测试获取zk信息失败!!!" self.assertEqual(zk_info["zkData"]["test"], 123), "测试获取zk失败, zk数据错误!!!"
def test_zk_info(self): """ 测试获取zk信息 :return: """ timestamp = get_now_time_timestamp(utc=True) md5 = md5_check(self.password, timestamp) req_data = { "username": self.username, "md5": md5, "timestamp": timestamp, "zkPath": self.zk_name } # 手动构建request req = request.HttpRequest() req.method = 'POST' for key, value in req_data.items(): req.POST.setdefault(key, value) resp = zookeeper(req) zk_info = json.loads(resp.content) self.assertEqual(zk_info["status"], 0), "测试获取zk信息失败!!!" self.assertEqual(zk_info["obj"]["test"], 123), "测试获取zk失败, zk数据错误!!!"
def get_moderator_group_set(node_group_set, curr_group_id, get_details=False): ''' Returns the "group_set". Takes two arguments: - node_group_set: existing/current group_set of node object. - curr_group_id: current group in which this node resides. Pass the deep copy of group_set and not the reference. e.g: updated_group_set = get_moderator_group_set(node_group_set[:], group_id) ------------ if there is need of extra information along with group_set, pass <get_details=True> as last arg. e.g: updated_group_set = get_moderator_group_set(node_group_set[:], group_id, get_details=True) Along with group_set following details will be returned in DICT format: { "updated_group_set": updated_group_set, "is_group_set_updated": is_group_set_updated, "removed_group_id": removed_group_id, "newly_appended_group_id": newly_appended_group_id, "newly_appended_group_name": newly_appended_group_name, "is_new_group_top_group": is_new_group_top_group } ''' curr_group_obj = node_collection.one({'_id': ObjectId(curr_group_id)}) group_set = node_group_set[:] is_group_set_updated = False is_new_group_top_group = False # initializing dict with defaults: details_dict = { "updated_group_set": group_set, "is_group_set_updated": is_group_set_updated, "removed_group_id": None, "newly_appended_group_id": None, "newly_appended_group_name": None, "is_new_group_top_group": is_new_group_top_group } # check if current group having edit policy of EDITABLE_MODERATED. # if no return group_set as it was if not curr_group_obj.edit_policy == 'EDITABLE_MODERATED': if get_details: return details_dict else: return node_group_set # ---| getting appropriate member_of group |--- # for top level of moderated group list_of_sg_member_of = get_sg_member_of(curr_group_obj._id) if "ModeratingGroup" in list_of_sg_member_of: member_of = node_collection.one({'_type': 'GSystemType', 'name': u'ModeratingGroup'}) elif 'ProgramEventGroup' in list_of_sg_member_of: member_of = node_collection.one({'_type': 'GSystemType', 'name': u'ProgramEventGroup'}) elif 'CourseEventGroup' in list_of_sg_member_of: member_of = node_collection.one({'_type': 'GSystemType', 'name': u'CourseEventGroup'}) # for sub-group falling under one of following categories: # elif curr_group_obj.member_of_names_list[0] in ['PartnerGroup', 'ModeratingGroup']: # member_of = node_collection.one({'_id': curr_group_obj.member_of[0]}) # final fallback option else: # GST of "ModeratingGroup" member_of = node_collection.one({'_type': 'GSystemType', 'name': u'ModeratingGroup'}) # getting sub-group having: # curr_group in prior_node # and member_of as fetched above # and moderation_level > -1 sub_mod_group_obj = node_collection.one({ '_type': 'Group', 'prior_node': {'$in': [ObjectId(curr_group_obj._id)]}, 'member_of': {'$in': [ObjectId(member_of._id)]}, 'moderation_level': {'$gt': -1} }) # print "curr_group_obj._id : ", curr_group_obj._id # print "member_of._id : ", member_of._id # print "sub_mod_group_obj.name : ", sub_mod_group_obj.name # proper sub-group found if sub_mod_group_obj: if ObjectId(curr_group_id) in group_set: # remove current group's _id removed_group_id = group_set.pop(group_set.index(ObjectId(curr_group_id))) if not ObjectId(sub_mod_group_obj._id) in group_set: # add next/sub-group's _id group_set.append(sub_mod_group_obj._id) newly_appended_group_id = sub_mod_group_obj._id.__str__() newly_appended_group_name = sub_mod_group_obj.name is_group_set_updated = True # if no sub-group found or it's last sub-group of hierarchy else: mod_group_instance = CreateModeratedGroup(request.HttpRequest()) is_top_group, top_group_obj = mod_group_instance.get_top_group_of_hierarchy(curr_group_id) # print "==== ", is_top_group # print "==== ", top_group_obj if ObjectId(curr_group_id) in group_set: # remove current group's _id removed_group_id = group_set.pop(group_set.index(ObjectId(curr_group_id))) if is_top_group and (not ObjectId(top_group_obj._id) in group_set): # add parent/top group's _id group_set.append(top_group_obj._id) newly_appended_group_id = top_group_obj._id.__str__() newly_appended_group_name = top_group_obj.name is_group_set_updated = True is_new_group_top_group = True if get_details: details_dict = { "updated_group_set": group_set, "is_group_set_updated": is_group_set_updated, "removed_group_id": removed_group_id, "newly_appended_group_id": newly_appended_group_id, "newly_appended_group_name": newly_appended_group_name, "is_new_group_top_group": is_new_group_top_group } return details_dict # print group_set return group_set