Exemple #1
0
    def test_invalid_input(self):
        with self.assertRaises(ValueError):
            Util.load_class_from_json(123)

        with self.assertRaises(ValueError):
            Util.load_class_from_json([])

        with self.assertRaises(ValueError):
            Util.load_class_from_json("")

        with self.assertRaises(ValueError):
            Util.load_class_from_json("Blub bla bla")

        with self.assertRaises(ValueError):
            Util.load_class_from_json(json.dumps({}))

        with self.assertRaises(ValueError):
            jsonStr = json.dumps(self.token1)
            data = json.loads(jsonStr)
            del data["type"]
            Util.load_class_from_json(json.dumps(data))

        with self.assertRaises(ValueError):
            Util.initialize_object_from_json(123)

        with self.assertRaises(ValueError):
            Util.initialize_object_from_json([])

        with self.assertRaises(ValueError):
            Util.initialize_object_from_json("")

        with self.assertRaises(ValueError):
            Util.initialize_object_from_json("blub bla bla")
Exemple #2
0
    def get(self, endpoint):
        """
        For convenience in this test suite.
        """
        data_result = []
        data = self.client.get(endpoint).json
        for d in data["list"]:
            data_result.append(Util.initialize_object_from_json(json.dumps(d)))

        return data_result
Exemple #3
0
    def test_initialize_object(self):
        self.assertEqual(
            Util.initialize_object_from_json(json.dumps(self.token1)),
            self.token1)
        self.assertEqual(
            Util.initialize_object_from_json(json.dumps(self.oauthtoken1)),
            self.oauthtoken1,
        )

        self.assertEqual(
            Util.initialize_object_from_json(json.dumps(self.service1)),
            self.service1)
        self.assertEqual(
            Util.initialize_object_from_json(json.dumps(self.oauthservice1)),
            self.oauthservice1,
        )

        self.assertEqual(
            Util.initialize_object_from_json(json.dumps(self.user1)),
            self.user1)
Exemple #4
0
    def test_get_tokens_for_user(self):

        self.client.post(
            "/service",
            data=json.dumps(self.oauthservice1),
            content_type="application/json",
        )
        self.client.post(
            "/service",
            data=json.dumps(self.oauthservice2),
            content_type="application/json",
        )
        self.client.post(
            "/service",
            data=json.dumps(self.oauthservice3),
            content_type="application/json",
        )

        # there have to be a user
        self.client.post("/user",
                         data=json.dumps(self.user1),
                         content_type="application/json")

        # no tokens there
        expected = {"length": 0, "list": []}

        self.assertEqual(self.client.get("/token").json, expected)

        # add a token to user
        expected = {"length": 1, "list": [self.token1]}

        result = self.client.post(
            f"/user/{self.user1.username}/token",
            data=json.dumps(self.token1),
            content_type="application/json",
        )
        self.assertEqual(result.status_code, 200, msg=result.json)

        # list compare doesn't work properly, so we have to iterate.
        for k, v in enumerate(self.get("/token")):
            self.assertEqual(v,
                             expected["list"][k],
                             msg="{} {}".format(v, expected["list"][k]))

        # should response with http code not equal to 200, because user has already a token for this service
        response = self.client.post(
            f"/user/{self.user1.username}/token",
            data=json.dumps(self.oauthtoken1),
            content_type="application/json",
        )
        self.assertNotEqual(response.status_code, 200, msg=result.json)

        # add an oauthtoken to user
        expected = {"length": 2, "list": [self.token1, self.token2]}

        self.client.post(
            f"/user/{self.user2.username}/token",
            data=json.dumps(self.oauthtoken2),
            content_type="application/json",
        )

        for k, v in enumerate(self.get("/token")):
            self.assertEqual(v,
                             expected["list"][k],
                             msg="{} {}".format(v, expected["list"][k]))

        # check, if we can find a token by its servicename
        index = self.oauthtoken1.servicename

        result = self.client.get(f"/user/{self.user1.username}/token/{index}")
        self.assertEqual(result.status_code,
                         200,
                         msg=f"token id: {index} - {result.json}")
        token = Util.initialize_object_from_json(result.get_data(as_text=True))
        self.assertEqual(token, self.oauthtoken1, msg=token)

        # check, if we can find a token by its index
        index = 0

        result = self.client.get(f"/user/{self.user1.username}/token/{index}")
        self.assertEqual(result.status_code,
                         200,
                         msg=f"token id: {index} - {result.json}")
        token = Util.initialize_object_from_json(result.get_data(as_text=True))
        self.assertEqual(token, self.token1, msg=token)

        expected = {"length": 1, "list": [self.token2]}
Exemple #5
0
    def test_user_tokens(self):

        self.client.post(
            "/service",
            data=json.dumps(self.oauthservice1),
            content_type="application/json",
        )
        self.client.post(
            "/service",
            data=json.dumps(self.oauthservice2),
            content_type="application/json",
        )
        self.client.post(
            "/service",
            data=json.dumps(self.oauthservice3),
            content_type="application/json",
        )

        req = self.client.get(f"/user/{self.user1.username}")
        self.assertEqual(req.status_code, 404)
        self.assertEqual(req.json["error"], "NotFound")
        self.assertEqual(req.json["http_code"], 404)

        req = self.client.get(f"/user/{self.user1.username}/token")
        self.assertEqual(req.status_code, 404, msg=req.data)
        self.assertEqual(req.json["error"], "NotFound")
        self.assertEqual(req.json["http_code"], 404)

        req = self.client.get(f"/user/{self.user1.username}/token/0")
        self.assertEqual(req.status_code, 404, msg=req.data)
        self.assertEqual(req.json["error"], "NotFound")
        self.assertEqual(req.json["http_code"], 404)

        result = self.client.post("/user",
                                  data=json.dumps(self.user1),
                                  content_type="application/json")
        self.assertEqual(result.status_code, 200)
        self.assertEqual(result.json, self.success)

        # we do not have to check here, if user is there, because it was checked in test_list_user.

        req = self.client.get(f"/user/{self.user1.username}/token/0")
        self.assertEqual(req.status_code, 404)
        self.assertEqual(req.json["error"], "NotFound")
        self.assertEqual(req.json["http_code"], 404)

        result = self.client.post(
            f"/user/{self.user1.username}/token",
            data=json.dumps(self.oauthtoken1),
            content_type="application/json",
        )
        self.assertEqual(result.status_code, 200, msg=result.json)
        self.assertEqual(result.json, self.success)

        req = self.client.get(f"/user/{self.user1.username}/token/0")
        self.assertEqual(req.status_code, 200)
        self.assertEqual(
            Util.initialize_object_from_json(req.get_data(as_text=True)),
            self.oauthtoken1,
        )

        result = self.client.post(
            f"/user/{self.user1.username}/token",
            data=json.dumps(self.oauthtoken1),
            content_type="application/json",
        )
        self.assertEqual(result.status_code, 409, msg=result.json)
        self.assertEqual(result.json["error"], "Conflict", msg=result.json)

        # test, if user will be created, if not exists
        result = self.client.post(
            f"/user/{self.user2.username}/token",
            data=json.dumps(self.oauthtoken2),
            content_type="application/json",
        )
        self.assertEqual(result.status_code, 201, msg=result.json)
        self.assertEqual(result.json, self.success)
        """ Placeholder for more
Exemple #6
0
    def test_list_user(self):
        expected = {"length": 0, "list": []}

        # no user should be there
        self.assertEqual(self.client.get("/user").json, expected)
        req = self.client.get(f"/user/{self.user1.username}")
        self.assertEqual(req.status_code, 404)
        self.assertEqual(req.json["error"], "NotFound")
        self.assertEqual(req.json["http_code"], 404)

        expected = {"length": 1, "list": [self.user1]}

        # add a user, then there should be a user
        result = self.client.post("/user",
                                  data=json.dumps(self.user1),
                                  content_type="application/json")

        self.assertEqual(result.status_code, 200, msg=result.json)
        self.assertEqual(result.json, {"success": True})

        for k, v in enumerate(self.get("/user")):
            self.assertEqual(v,
                             expected["list"][k],
                             msg="{} {}".format(v, expected["list"][k]))

        req = self.client.get(f"/user/{self.user1.username}")
        self.assertEqual(req.status_code, 200)

        # get the added user
        d = self.client.get(f"/user/{self.user1.username}")
        self.assertEqual(
            Util.initialize_object_from_json(
                json.dumps(d.get_data(as_text=True))),
            self.user1,
        )

        # add a new user and check
        expected = {"length": 2, "list": [self.user1, self.user2]}

        self.client.post("/user",
                         data=json.dumps(self.user2),
                         content_type="application/json")
        for k, v in enumerate(self.get("/user")):
            self.assertEqual(v,
                             expected["list"][k],
                             msg="{} {}".format(v, expected["list"][k]))

        # the first user should be there
        d = self.client.get(f"/user/{self.user1.username}")
        self.assertEqual(
            Util.initialize_object_from_json(
                json.dumps(d.get_data(as_text=True))),
            self.user1,
        )

        # remove a user
        expected = {"length": 1, "list": [self.user2]}

        result = self.client.delete(f"/user/{self.user1.username}")
        self.assertEqual(result.status_code, 200)
        for k, v in enumerate(self.get("/user")):
            self.assertEqual(v,
                             expected["list"][k],
                             msg="{} {}".format(v, expected["list"][k]))

        self.assertEqual(
            self.client.get(f"/user/{self.user1.username}").status_code, 404)

        result = self.client.delete(f"/user/{self.user1.username}")
        self.assertEqual(result.status_code, 404)