Beispiel #1
0
    def test_change_user_password(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()

        user = utl.USERS[-1]
        # Test changing the password with an invalid old password
        res = self.client.patch(
            "/api/users/{}/password".format(user["id"]),
            json={
                "old": "failure",
                "new": "failure"
            },
        )
        self.assertEqual(res.status_code, 400)

        # Test changing the password with the correct old password
        res = self.client.patch(
            "/api/users/{}/password".format(user["id"]),
            json={
                "old": user["password"],
                "new": "failure"
            },
        )
        self.assertEqual(res.status_code, 204)
Beispiel #2
0
    def test_get_multiple(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()

        # Test getting multiple users
        res = self.client.get("/api/users")
        self.assertEqual(res.status_code, 200, res.get_json())
Beispiel #3
0
    def test_get_user_sensors(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()

        # Test getting sensors from a user that doesn't exist
        res = self.client.get("/api/users/42/sensors")
        self.assertEqual(res.status_code, 404, res.get_json())

        user = utl.USERS[-1]
        # Test getting sensors from a user that does exist
        res = self.client.get("/api/users/{}/sensors".format(user["id"]))
        self.assertEqual(res.status_code, 200, res.get_json())
Beispiel #4
0
    def test_remove_user_sensors(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()

        user = utl.USERS[-1]
        # Test removing sensors from a user
        res = self.client.patch(
            "/api/users/{}/sensors/remove".format(user["id"]),
            json={"sensors": user["sensors"]},
        )
        self.assertEqual(res.status_code, 204)

        # Test removing an invalid sensor
        res = self.client.patch(
            "/api/users/{}/sensors/remove".format(user["id"]),
            json={"sensors": ["failure"]},
        )
        self.assertEqual(res.status_code, 400)
Beispiel #5
0
    def test_get_check_and_revoke_token(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()
        user = User.get_all()[-1]

        # Check that a token gets registered to the user
        token = user.get_token()
        self.assertEqual(token, user.token)

        # Check that we recieve the same token when requesting within the timeout
        self.assertEqual(token, user.get_token())

        # Test that the check_token method works
        self.assertEqual(User.check_token(token), user)

        # Test that the revoke_token method works
        user.revoke_token()
        self.assertEqual(User.check_token(token), None)
        self.assertNotEqual(user.get_token(), token)
Beispiel #6
0
    def test_get_token(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()
        user = utl.USERS[-1]

        # Test that we can login with the correct credentials
        login_info = (user["name"] + ":" + user["password"]).encode()
        res = self.client.get(
            "/api/tokens",
            headers={
                "Authorization":
                "Basic " + base64.b64encode(login_info).decode("utf-8")
            },
        )
        self.assertEqual(res.status_code, 200, res.get_json())

        # Test that we get an error when accessing the route without auth
        res = self.client.get("/api/tokens")
        self.assertEqual(res.status_code, 401, res.get_json())
Beispiel #7
0
    def test_add_user_sensors(self):
        utl.add_categories_to_db()
        utl.add_sensors_to_db()
        utl.add_users_to_db()
        valid_sensors = [sensor["name"] for sensor in utl.SENSORS]

        user = utl.USERS[-1]
        # Test adding sensors to a user
        missing_sensors = [
            sensor for sensor in valid_sensors if sensor not in user["sensors"]
        ]
        res = self.client.patch(
            "/api/users/{}/sensors/add".format(user["id"]),
            json={"sensors": missing_sensors},
        )
        self.assertEqual(res.status_code, 204)

        # Test adding a sensor that doesn't exist
        res = self.client.patch(
            "/api/users/{}/sensors/add".format(user["id"]),
            json={"sensors": ["failure"]},
        )
        self.assertEqual(res.status_code, 400)