def test_create(self): utl.add_categories_to_db() utl.add_sensors_to_db() # Test adding a user with one missing parameter res = self.client.post("/api/users", json={"name": "failure"}) self.assertEqual(res.status_code, 400, res.get_json()) res = self.client.post("/api/users", json={"sensors": ["failure"]}) self.assertEqual(res.status_code, 400, res.get_json()) user = utl.USERS[-1] # Test adding a user with the correct parameters res = self.client.post("/api/users", json=user) self.assertEqual(res.status_code, 201, res.get_json()) # Test adding a user that's already in the database res = self.client.post("/api/users", json=user) self.assertEqual(res.status_code, 400, res.get_json()) # Test adding a user with a sensor that doesn't exist res = self.client.post("/api/users", json={ "name": "failure", "sensors": ["failure"] }) self.assertEqual(res.status_code, 400, res.get_json())
def test_from_and_to_dict(self): utl.add_categories_to_db() category = utl.CATEGORIES[-1] utl.add_sensors_to_db() sensor = utl.SENSORS[-1] r = Reading() reading = { "data": 123, "category": category["name"], "sensor": sensor["name"], "timestamp": 123, } # Test the from_dict() method r.from_dict(reading) self.assertEqual(r.data, reading["data"]) self.assertEqual(r.category.name, reading["category"]) self.assertEqual(r.sensor.name, reading["sensor"]) self.assertEqual(r.timestamp, reading["timestamp"]) # Test the to_dict() method r_dict = r.to_dict() self.assertEqual(r_dict["data"], reading["data"]) self.assertEqual(r_dict["category"]["name"], reading["category"]) self.assertEqual(r_dict["sensor"]["name"], reading["sensor"]) self.assertEqual(r_dict["timestamp"], reading["timestamp"])
def test_get_multiple(self): utl.add_categories_to_db() utl.add_sensors_to_db() # Get the list of sensor stored in the db res = self.client.get("/api/sensors") self.assertEqual(res.status_code, 200, res.get_json())
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)
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())
def test_add_category(self): utl.add_categories_to_db() category = utl.CATEGORIES[-1] r = Reading() # Test adding a category to a reading r.add_category(category["name"]) self.assertEqual(r.category.name, category["name"])
def test_add_sensor(self): utl.add_categories_to_db() utl.add_sensors_to_db() sensor = utl.SENSORS[-1] r = Reading() # Test adding a sensor to a reading r.add_sensor(sensor["name"]) self.assertEqual(r.sensor.name, sensor["name"])
def test_get_single(self): utl.add_categories_to_db() # Test getting a category that doesn't exist in the db res = self.client.get("/api/categories/42") self.assertEqual(res.status_code, 404, res.get_json()) # Test getting a category that has been created res = self.client.get("/api/categories/1") self.assertEqual(res.status_code, 200, res.get_json())
def test_get_single(self): utl.add_categories_to_db() utl.add_sensors_to_db() # Test getting a sensor that hasn't been created yet res = self.client.get("/api/sensors/42") self.assertEqual(res.status_code, 404, res.get_json()) # Test getting a sensor that has been created res = self.client.get("/api/sensors/1") self.assertEqual(res.status_code, 200, res.get_json())
def test_get_sensor_categories(self): utl.add_categories_to_db() utl.add_sensors_to_db() # Test getting categories from a sensor that doesn't exist res = self.client.get("/api/sensors/42/categories") self.assertEqual(res.status_code, 404, res.get_json()) # Test getting categories from a sensor that does exist res = self.client.get("/api/sensors/1/categories") self.assertEqual(res.status_code, 200, res.get_json())
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())
def test_get_multiple(self): utl.add_categories_to_db() utl.add_sensors_to_db() utl.add_readings_to_db() # Test getting readings from a sensor that doesn't exist res = self.client.get("api/sensors/42/readings") self.assertEqual(res.status_code, 404, res.get_json()) sensor = utl.SENSORS[-1] # Test getting readings from a valid sensor res = self.client.get("api/sensors/{}/readings".format(sensor["id"])) self.assertEqual(res.status_code, 200, res.get_json())
def test_add_and_remove_categories(self): utl.add_categories_to_db() sensor = utl.SENSORS[-1] s = Sensor() # Test adding categories to a sensor s.add_categories(sensor["categories"]) self.assertEqual(set([category.name for category in s.categories]), set(sensor["categories"])) self.assertEqual(len(s.categories), len(sensor["categories"])) s.remove_categories(sensor["categories"]) self.assertEqual(len(s.categories), 0)
def test_add_and_remove_sensors(self): utl.add_categories_to_db() utl.add_sensors_to_db() user = utl.USERS[-1] u = User() # Test adding sensors to a user u.add_sensors(user["sensors"]) self.assertEqual(set([sensor.name for sensor in u.sensors]), set(user["sensors"])) self.assertEqual(len(u.sensors), len(user["sensors"])) # Test removing sensors from a user u.remove_sensors(user["sensors"]) self.assertEqual(len(u.sensors), 0)
def test_are_valid_categories(self): utl.add_categories_to_db() # Test checking an array with valid categories categories = [category["name"] for category in utl.CATEGORIES] valid = Category.are_valid_categories(categories) self.assertEqual(valid, True) categories.append("failure") # Test checking an array with one invalid category valid = Category.are_valid_categories(categories) self.assertEqual(valid, False) # Test checking an empty list valid = Category.are_valid_categories([]) self.assertEqual(valid, False)
def test_are_valid_sensors(self): utl.add_categories_to_db() utl.add_sensors_to_db() # Test checking an array with valid sensors sensors = [sensor["name"] for sensor in utl.SENSORS] valid = Sensor.are_valid_sensors(sensors) self.assertEqual(valid, True) sensors.append("failure") # Test checking an array with one invalid sensor valid = Sensor.are_valid_sensors(sensors) self.assertEqual(valid, False) # Test checking an empty list valid = Sensor.are_valid_sensors([]) self.assertEqual(valid, False)
def test_remove_sensor_categories(self): utl.add_categories_to_db() utl.add_sensors_to_db() sensor = utl.SENSORS[-1] # Test removing available categories from sensors res = self.client.patch( "/api/sensors/{}/categories/remove".format(sensor["id"]), json={"categories": sensor["categories"]}, ) self.assertEqual(res.status_code, 204) # Test removing a category that doesn't exist res = self.client.patch( "/api/sensors/{}/categories/remove".format(sensor["id"]), json={"categores": ["failure"]}, ) self.assertEqual(res.status_code, 400)
def test_from_and_to_dict(self): utl.add_categories_to_db() sensor = utl.SENSORS[-1] s = Sensor() # Test using the from_dict() method s.from_dict(sensor) self.assertEqual(s.name, sensor["name"]) self.assertEqual(set([category.name for category in s.categories]), set(sensor["categories"])) # Test using the to_dict() method s_dict = s.to_dict() self.assertEqual(s_dict["name"], sensor["name"]) self.assertEqual( set([category["name"] for category in s_dict["categories"]]), set(sensor["categories"]), )
def test_create(self): utl.add_categories_to_db() utl.add_sensors_to_db() # Test adding a reading to a sensor that doesn't exist res = self.client.post("api/sensors/42/readings", json={ "data": 404, "category": "temperature" }) self.assertEqual(res.status_code, 404, res.get_json()) sensor = utl.SENSORS[-1] # Test adding a reading with a category that doesn't exist res = self.client.post( "api/sensors/{}/readings".format(sensor["id"]), json={ "data": 400, "category": "failure" }, ) self.assertEqual(res.status_code, 400, res.get_json()) # Test adding a reading with no data parameter res = self.client.post("api/sensors/{}/readings".format(sensor["id"]), json={"category": "failure"}) self.assertEqual(res.status_code, 400, res.get_json()) # Test adding a reading with no category parameter res = self.client.post("api/sensors/{}/readings".format(sensor["id"]), json={"data": 400}) self.assertEqual(res.status_code, 400, res.get_json()) # Add valid readings for category in sensor["categories"]: reading = { "data": randint(utl.READING_LOWER_LIMIT, utl.READING_UPPER_LIMIT), "category": category, } res = self.client.post("/api/sensors/{}/readings".format( sensor["id"]), json=reading) self.assertEqual(res.status_code, 201, res.get_json())
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)
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())
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)
def test_from_and_to_dict(self): utl.add_categories_to_db() utl.add_sensors_to_db() user = utl.USERS[-1] u = User() # Test converting from dict to object u.from_dict(user) self.assertEqual(u.name, user["name"]) self.assertTrue(check_password_hash(u.password_hash, user["password"])) self.assertEqual(set([sensor.name for sensor in u.sensors]), set(user["sensors"])) # Test converting from object to dict u_dict = u.to_dict() self.assertEqual(u_dict["name"], user["name"]) self.assertTrue( check_password_hash(u_dict["password_hash"], user["password"])) self.assertEqual(set([sensor["name"] for sensor in u_dict["sensors"]]), set(user["sensors"]))
def test_create(self): utl.add_categories_to_db() # Test adding a sensor with one missing parameter res = self.client.post("/api/sensors", json={"name": "failure"}) self.assertEqual(res.status_code, 400, res.get_json()) sensor = utl.SENSORS[-1] # Test adding sensors with the correct parameters res = self.client.post("/api/sensors", json=sensor) self.assertEqual(res.status_code, 201, res.get_json()) # Test adding a sensor that's already been added res = self.client.post("/api/sensors", json=sensor) self.assertEqual(res.status_code, 400, res.get_json()) # Test adding a sensor with a category that doesn't exist in db res = self.client.post( "/api/sensors", json={"name": "failure", "categories": ["failure"]} ) self.assertEqual(res.status_code, 400, res.get_json())
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)
def test_add_sensor_categories(self): utl.add_categories_to_db() utl.add_sensors_to_db() valid_categories = [category["name"] for category in utl.CATEGORIES] sensor = utl.SENSORS[-1] # Test adding missing categories to sensors missing_categories = [ category for category in valid_categories if category not in sensor["categories"] ] res = self.client.patch( "/api/sensors/{}/categories/add".format(sensor["id"]), json={"categories": missing_categories}, ) self.assertEqual(res.status_code, 204) # Test adding a category that doesn't exist res = self.client.patch( "/api/sensors/{}/categories/add".format(sensor["id"]), json={"categories": ["failure"]}, ) self.assertEqual(res.status_code, 400)
def test_get_single(self): utl.add_categories_to_db() utl.add_sensors_to_db() utl.add_readings_to_db() # Test getting a reading from a sensor that doesn't exist res = self.client.get("api/sensors/42/readings/1") self.assertEqual(res.status_code, 404, res.get_json()) sensor = utl.SENSORS[-1] # Test getting a reading that doesn't exist res = self.client.get("api/sensors/{}/readings/42".format( sensor["id"])) self.assertEqual(res.status_code, 404, res.get_json()) readings = [ reading["id"] for reading in utl.READINGS if reading["sensor"] == sensor["id"] ] # Test getting readings that do exist for reading in readings: res = self.client.get("api/sensors/{}/readings/{}".format( sensor["id"], reading)) self.assertEqual(res.status_code, 200, res.get_json())
def test_get_multiple(self): utl.add_categories_to_db() # Test getting multiple categories from the db res = self.client.get("/api/categories") self.assertEqual(res.status_code, 200, res.get_json())