def test_server_connection_error(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"donations": []}, status_code=status.HTTP_500_INTERNAL_SERVER_ERROR) req = Requirement(name="Contributor +1 Requirement", url='%s/profiles/<username>/' % self.api_endpoint, key="donations", operator="coperator\ntruth\np0\n.", condition="[...]") req.save() try: req.check_requirement(self.user.username) except IOError as e: self.assertIsInstance(e, IOError) self.assertEqual(str(e), '500') req.delete()
def create_badges(self): with open( '%s/sb_badges/management/commands' '/badge_nodes.json' % settings.PROJECT_DIR, 'r') as data_file: data = loads(data_file.read()) for badge in data: requirements = badge.pop('requirements', []) badge = Badge(**badge).save() for requirement in requirements: try: req = Requirement(**requirement).save() badge.requirements.connect(req) except (CypherException, IOError): continue
def setUp(self): self.email = "*****@*****.**" self.password = "******" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.privilege = Privilege(name=str(uuid1())).save() self.requirement = Requirement(name=str(uuid1()), url=settings.WEB_ADDRESS + "/v1/profiles/" "<username>/reputation/", key="reputation", operator="coperator\nge\np0\n.", condition=0).save() self.privilege.requirements.connect(self.requirement)
def test_equal_operator_fail_string(self, m): m.get('%s/profiles/%s/testing/' % (self.api_endpoint, self.pleb.username), json={"test": "goodbye"}, status_code=status.HTTP_200_OK) req = Requirement(name="Total Rep Key Test Hello", url='%s/profiles/<username>/testing/' % self.api_endpoint, key="test", operator='coperator\neq\np0\n.', condition="hello") req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) req.delete()
def test_is_operator_pass(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"active": True}, status_code=status.HTTP_200_OK) req = Requirement(name="Is Active", url='%s/profiles/<username>/' % self.api_endpoint, key="active", operator='coperator\nis_\np0\n.', condition=True) req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'is') self.assertEqual(result['detail'], 'The requirement Is Active was met') self.assertEqual(result['key'], 'active') req.delete()
def test_not_operator_pass_none(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"flag": None}, status_code=status.HTTP_200_OK) req = Requirement(name="Flag is False", url='%s/profiles/<username>/' % self.api_endpoint, key="flag", operator='coperator\nnot_\np0\n.') req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'not') self.assertEqual(result['detail'], 'The requirement Flag is False' ' was met') self.assertEqual(result['key'], 'flag') req.delete()
def test_non_username_url(self, m): m.get('%s/profiles/reputation/' % self.api_endpoint, json={"reputation": 0}, status_code=status.HTTP_200_OK) req = Requirement(name="Total Rep 0", url='%s/profiles/reputation/' % self.api_endpoint, key="reputation", operator='coperator\neq\np0\n.', condition=0) req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'equal to') self.assertEqual(result['detail'], 'The requirement Total Rep 0 was ' 'met') self.assertEqual(result['key'], 'reputation') req.delete()
def test_is_operator_fail_none(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"active": None}, status_code=status.HTTP_200_OK) req = Requirement(name="Is Active", url='%s/profiles/<username>/' % self.api_endpoint, key="active", operator='coperator\nis_\np0\n.', condition=True) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual( result['detail'], "We're sorry we cannot check this " "requirement. We cannot compare a " "null type in this case. Please " "try using 'is not' instead.") req.delete()
def test_is_not_operator_pass(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"campaign": "john_apple"}, status_code=status.HTTP_200_OK) req = Requirement(name="Quest Subscriber Requirement", url='%s/profiles/<username>/' % self.api_endpoint, key="campaign", operator="coperator\nis_not\np0\n.", condition=None) req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'is not') self.assertEqual( result['detail'], 'The requirement Quest Subscriber ' 'Requirement was met') self.assertEqual(result['key'], 'campaign') req.delete()
def test_non_json_response(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), text="hello this is not json :)", status_code=status.HTTP_200_OK) req = Requirement(name="Contributor +1 Requirement", url='%s/profiles/<username>/' % self.api_endpoint, key="donations", operator="coperator\ntruth\np0\n.", condition="[...]") req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual( result['detail'], "We're sorry we cannot check this " "requirement. It looks like the url " "you're checking doesn't return " "a JSON response.") req.delete()
def test_not_operator_fail(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"flag": True}, status_code=status.HTTP_200_OK) req = Requirement(name="Flag is False", url='%s/profiles/<username>/' % self.api_endpoint, key="flag", operator='coperator\nnot_\np0\n.', condition=None) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'not') self.assertEqual( result['detail'], 'You have True flag, flag ' 'must be not None to gain the ' 'Flag is False Privilege.') self.assertEqual(result['key'], 'flag') req.delete()
def test_not_equal_operator_pass_string(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"test": "hello"}, status_code=status.HTTP_200_OK) req = Requirement(name="Not Equal to 10 Rep", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="test", operator='coperator\nne\np0\n.', condition="goodbye") req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'not have') self.assertEqual(result['detail'], 'The requirement Not Equal to 10 ' 'Rep was met') self.assertEqual(result['key'], 'test') req.delete()
def test_greater_than_operator_pass(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 15}, status_code=status.HTTP_200_OK) req = Requirement(name="More Than 10 Rep", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\ngt\np0\n.', condition=10) req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'more than') self.assertEqual(result['detail'], 'The requirement More Than 10 Rep ' 'was met') self.assertEqual(result['key'], 'reputation') req.delete()
def test_is_operator_fail(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"active": False}, status_code=status.HTTP_200_OK) req = Requirement(name="Is Active", url='%s/profiles/<username>/' % self.api_endpoint, key="active", operator='coperator\nis_\np0\n.', condition=True) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'is') self.assertEqual( result['detail'], 'You have False active, active ' 'must be is True to gain the Is ' 'Active Privilege.') self.assertEqual(result['key'], 'active') req.delete()
def test_is_not_operator_fail(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"campaign": None}, status_code=status.HTTP_200_OK) req = Requirement(name="Quest Subscriber Requirement", url='%s/profiles/<username>/' % self.api_endpoint, key="campaign", operator="coperator\nis_not\np0\n.", condition=None) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'is not') self.assertEqual( result['detail'], 'You have None campaign, campaign ' 'must be is not None to gain the ' 'Quest Subscriber Requirement' ' Privilege.') self.assertEqual(result['key'], 'campaign') req.delete()
def test_invalid_condition(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 5}, status_code=status.HTTP_200_OK) req = Requirement(name="Total Rep 0", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\neq\np0\n.', condition="hello") req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual( result['detail'], "We're sorry we cannot check this " "requirement. The condition is not " "the same type as the value it's " "being compared to.") req.delete()
def test_invalid_operator(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 5}, status_code=status.HTTP_200_OK) req = Requirement(name="Total Rep 0", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='op\neq\np0\n.', condition=0) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual( result['detail'], "We're sorry we cannot check this " "requirement. The" " operator does not seem to be " "valid.") req.delete()
def test_truth_operator_fail(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"donations": []}, status_code=status.HTTP_200_OK) req = Requirement(name="Contributor +1 Requirement", url='%s/profiles/<username>/' % self.api_endpoint, key="donations", operator="coperator\ntruth\np0\n.", condition="[...]") req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'truth') self.assertEqual( result['detail'], 'You have [] donations, donations ' 'must be truth [...] to gain the ' 'Contributor +1 Requirement ' 'Privilege.') self.assertEqual(result['key'], 'donations') req.delete()
def test_truth_operator_pass(self, m): m.get('%s/profiles/%s/' % (self.api_endpoint, self.pleb.username), json={"donations": [ "7c8a59a1-847c-42e0-b178-6d1deac01d5f", ]}, status_code=status.HTTP_200_OK) req = Requirement(name="Contributor +1 Requirement", url='%s/profiles/<username>/' % self.api_endpoint, key="donations", operator="coperator\ntruth\np0\n.", condition="[...]") req.save() result = req.check_requirement(self.user.username) self.assertTrue(result['response']) self.assertEqual(result['operator'], 'truth') self.assertEqual( result['detail'], 'The requirement Contributor +1' ' Requirement was met') self.assertEqual(result['key'], 'donations') req.delete()
def test_invalid_key(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"badkey": 15}, status_code=status.HTTP_200_OK) req = Requirement(name="Not Equal to 10 Rep", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="goodkey", operator='coperator\nne\np0\n.', condition=10) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual( result['detail'], "We're sorry we cannot check " "this requirement." " The key cannot be found in the " "response we received from the " "server.") req.delete()
def test_equal_operator_fail(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 5}, status_code=status.HTTP_200_OK) req = Requirement(name="Total Rep 0", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\neq\np0\n.', condition=0) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'equal to') self.assertEqual( result['detail'], 'You have 5 reputation, reputation ' 'must be equal to 0 to gain the ' 'Total Rep 0 Privilege.') self.assertEqual(result['key'], 'reputation') req.delete()
def test_not_equal_operator_fail(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 10}, status_code=status.HTTP_200_OK) req = Requirement(name="Not Equal to 10 Rep", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\nne\np0\n.', condition=10) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'not have') self.assertEqual( result['detail'], 'You have 10 reputation, ' 'reputation must be not have ' '10 to gain the Not Equal to ' '10 Rep Privilege.') self.assertEqual(result['key'], 'reputation') req.delete()
def test_not_equal_operator_fail_string(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"test": "hello"}, status_code=status.HTTP_200_OK) req = Requirement(name="Not Equal to 10 Rep", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="test", operator='coperator\nne\np0\n.', condition="hello") req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'not have') self.assertEqual( result['detail'], 'You have hello test, test ' 'must be not have hello to gain ' 'the Not Equal to 10 Rep ' 'Privilege.') self.assertEqual(result['key'], 'test') req.delete()
def test_less_than_operator_fail(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 15}, status_code=status.HTTP_200_OK) req = Requirement(name="Less than 10 Rep", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\nlt\np0\n.', condition=10) req.save() result = req.check_requirement(self.user.username) self.assertFalse(result['response']) self.assertEqual(result['operator'], 'less than') self.assertEqual( result['detail'], 'You have 15 reputation, ' 'reputation must be less than 10 ' 'to gain the Less than 10 Rep ' 'Privilege.') self.assertEqual(result['key'], 'reputation') req.delete()
def setUp(self): query = "MATCH (n) OPTIONAL MATCH (n)-[r]-() DELETE n, r" db.cypher_query(query) cache.clear() self.email = "*****@*****.**" self.password = "******" self.pleb = create_user_util_test(self.email) self.user = User.objects.get(email=self.email) self.username = self.pleb.username self.privilege_name = str(uuid1()) self.privilege = Privilege(name=self.privilege_name).save() self.requirement = Requirement(name=str(uuid1()), url=settings.WEB_ADDRESS + "/v1/profiles/" "<username>/reputation/", key="reputation", operator="coperator\nge\np0\n.", condition=30).save() self.action = SBAction(resource=str(uuid1()), url="/v1/comments/").save() self.privilege.actions.connect(self.action) self.action.privilege.connect(self.privilege) self.test_url = settings.WEB_ADDRESS self.privilege.requirements.connect(self.requirement)
def test_do_not_get_privilege(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 10}, status_code=status.HTTP_200_OK) req = Requirement(name="Total Rep 0 Priv Test", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\neq\np0\n.', condition=0) req.save() privilege = Privilege(name="Awesome Privilege") privilege.save() privilege.requirements.connect(req) result = privilege.check_requirements(self.pleb.username) privilege.requirements.disconnect(req) privilege.delete() req.delete() self.assertFalse(result)
def test_do_not_get_privilege_bad_request(self, m): m.get('%s/profiles/%s/reputation/' % (self.api_endpoint, self.pleb.username), json={"reputation": 0}, status_code=status.HTTP_400_BAD_REQUEST) req = Requirement(name="Total Rep 0 Priv Test", url='%s/profiles/<username>/reputation/' % self.api_endpoint, key="reputation", operator='coperator\neq\np0\n.', condition=0) req.save() privilege = Privilege(name="Awesome Privilege") privilege.save() privilege.requirements.connect(req) try: privilege.check_requirements(self.pleb.username) except IOError as e: privilege.requirements.disconnect(req) privilege.delete() req.delete() self.assertIsInstance(e, IOError) self.assertEqual(str(e), '400')