Esempio n. 1
0
 def test_get_protocol_by_protocol_id(self):
     p = Protocol(value='Turn off the lights')
     db_session.add(p)
     db_session.commit()
     raw = self.app.get('/api/protocol/{}'.format(p.protocol_id))
     resp = json.loads(raw.get_data())
     self.assertEqual(p.to_json(), resp)
Esempio n. 2
0
 def test_get_building_by_name(self):
     b = Building(name='Shelby')
     db_session.add(b)
     db_session.commit()
     resp = json.loads(
         self.app.get('/api/building/{}'.format(b.name)).get_data())
     self.assertEqual(resp, b.to_json())
Esempio n. 3
0
def add_remove_protocol_family_association(family_id, protocol_id):
    family = ProtocolFamily.query.filter(
        ProtocolFamily.family_id == family_id).first()
    if family is None:
        return _error('Protocol family does not exist', 404)
    protocol = Protocol.query.filter(
        Protocol.protocol_id == protocol_id).first()
    if protocol is None:
        return _error('Protocol does not exists', 404)
    pfp = ProtocolFamilyProtocol.query.filter(
        ProtocolFamilyProtocol.family_id == family.family_id,
        ProtocolFamilyProtocol.protocol_id == protocol.protocol_id).first()
    if request.method == 'POST':
        if pfp is not None:
            return _error('Protocol already exists in specified family', 400)
        pfp = ProtocolFamilyProtocol(family.family_id, protocol.protocol_id)
        db_session.add(pfp)
        db_session.commit()
        return jsonify(pfp.to_json())
    # Must be a delete
    if pfp is None:
        return _error('Protocol does not exist in specified family', 404)
    to_delete = pfp.to_json()
    db_session.delete(pfp)
    db_session.commit()
    return jsonify(to_delete)
Esempio n. 4
0
def connect_part_protocol(element_id, protocol_id):
    part = Part.query.filter(Part.element_id == element_id).first()
    if part is None:
        return _error('Part element_id does not exist', 404)
    protocol = Protocol.query.filter(
        Protocol.protocol_id == protocol_id).first()
    if protocol is None:
        return _error('Protocol protocol_id does not exist', 404)
    pp = (PartProtocol.query.filter(
        PartProtocol.part_id == part.part_id,
        PartProtocol.protocol_id == protocol_id).first())
    if request.method == 'POST':
        if pp is not None:
            return _error('Protocol is already associated with that part', 400)
        pp = PartProtocol(part_id=part.part_id, protocol_id=protocol_id)
        db_session.add(pp)
        db_session.commit()
        return jsonify(pp.to_json())
    # Must be a delete
    if pp is None:
        return _error('Part protocol relationship does not exist', 404)
    to_delete = pp.to_json()
    db_session.delete(pp)
    db_session.commit()
    return jsonify(to_delete)
Esempio n. 5
0
def user_info():
    if request.method == 'GET':
        return jsonify(current_user.to_json())
    if not request.is_json:
        return _error('Request must be JSON type', 400)
    form = request.get_json()
    hashed = bcrypt.hashpw(form['password'].encode('utf-8'), bcrypt.gensalt())
    user = User.query.filter(User.email == form['email']
                             or User.id == form['_id']).first()
    print(user)
    if request.method == 'PUT':
        if user is not None:
            return _error('An account with that email already exists', 400)
        new_user = User(first_name=form.get('first_name'),
                        last_name=form.get('last_name'),
                        email=form['email'],
                        password=hashed)
        db_session.add(new_user)
        db_session.commit()
        return jsonify(new_user.to_json())
    elif request.method == 'POST':
        if user is None:
            return _error('Specified user not found', 400)
        user.first_name = form.get('first_name')
        user.last_name = form.get('last_name')
        user.email = form.get('email')
        if form.get('password') is not None:
            user.password = hashed
        db_session.add(user)
        db_session.commit()
        return jsonify(user.to_json())
    return _error('Invalid request type.', 400)
Esempio n. 6
0
 def test_delete_protocol_family_successful(self):
     db_session.add(self.family1)
     db_session.commit()
     q_str = '/api/protocol-family/{}'.format(self.family1.family_id)
     self.app.delete(q_str)
     fam = ProtocolFamily.query.first()
     self.assertIsNone(fam)
Esempio n. 7
0
 def test_add_family_protocol_association_nonexistent_protocol(self):
     db_session.add_all([self.family1])
     db_session.commit()
     q_str = '/api/protocol-family/{}/protocol/{}'.format(
         self.family1.family_id, 999)
     raw = self.app.post(q_str)
     self.assertEqual(404, raw.status_code)
Esempio n. 8
0
 def test_get_protocol_families(self):
     db_session.add_all([self.family1, self.family2])
     db_session.commit()
     resp = json.loads(self.app.get('/api/protocol-family').data)
     self.assertEqual(resp,
                      [self.family1.to_json(),
                       self.family2.to_json()])
Esempio n. 9
0
 def test_delete_protocol(self):
     db_session.add(self.protocol1)
     db_session.commit()
     pid = self.protocol1.protocol_id
     self.app.delete('/api/protocol/{}'.format(pid))
     deleted = Protocol.query.filter(Protocol.protocol_id == pid).first()
     self.assertTrue(deleted is None)
Esempio n. 10
0
 def test_delete_family_protocol_association_nonexistent_association(self):
     db_session.add_all([self.protocol1, self.family1])
     db_session.commit()
     q_str = '/api/protocol-family/{}/protocol/{}'.format(
         self.family1.family_id, self.protocol1.protocol_id)
     raw = self.app.delete(q_str)
     self.assertEqual(raw.status_code, 404)
Esempio n. 11
0
 def test_delete_building(self):
     db_session.add(self.building1)
     db_session.commit()
     bid = self.building1.building_id
     self.app.delete('/api/building/{}'.format(bid))
     deleted = Building.query.filter(Building.building_id == bid).first()
     self.assertTrue(deleted is None)
Esempio n. 12
0
def get_building_by_identifier(building_id=None, name=None):
    # Validate that the building exists
    if building_id is not None:
        # Lookup by id
        building = (Building.query.filter(
            Building.building_id == building_id).first())
    else:
        # Lookup by name
        building = (Building.query.filter(Building.name == name).first())
    if building is None:
        return _error('The specified building does not exist', 404)
    if request.method == 'DELETE':
        bid = building.building_id
        db_session.delete(building)
        db_session.commit()
        return jsonify({'building_id': bid})
    if request.method == 'PUT':
        if not request.is_json:
            return _error('Request must be JSON type', 400)
        form = request.get_json()
        if 'name' not in form:
            return _error('Name key not specified', 400)
        if not set(form.keys()).issubset({'name'}):
            return _error('Extra keys specified', 400)
        building.name = form['name']
        db_session.commit()
    return jsonify(building.to_json())
Esempio n. 13
0
def part_info(element_id):
    part = Part.query.filter(Part.element_id == element_id).first()
    if part is None:
        return _error('Part not found', 404)
    if request.method == 'DELETE':
        part_id = part.part_id
        db_session.delete(part)
        db_session.commit()
        return jsonify({'part_id': part_id})
    if request.method == 'PUT':
        if not request.is_json:
            return _error('Request must be JSON type', 400)
        form = request.get_json()
        if 'building_id' in form and form['building_id'] is not None:
            # Validate that the building exists
            building = (Building.query.filter(
                Building.building_id == form['building_id']).first())
            if building is None:
                return _error('Specified building does not exist', 404)
        if not set(form.keys()).issubset(part.to_json().keys()):
            return _error('Unknown properties specified in request', 400)
        part.element_id = form.get('element_id')
        part.building_id = form.get('building_id')
        part.part_name = form.get('part_name')
        db_session.add(part)
        db_session.commit()
    return jsonify(part.to_json())
Esempio n. 14
0
 def test_edit_protocol_family_not_json(self):
     db_session.add(self.family1)
     db_session.commit()
     q_str = '/api/protocol-family/{}'.format(self.family1.family_id)
     raw = self.app.put(q_str,
                        content_type='text/html',
                        data='this is not json')
     self.assertEqual(raw.status_code, 400)
Esempio n. 15
0
 def test_put_building_not_json(self):
     db_session.add(self.building1)
     db_session.commit()
     raw = self.app.put('/api/building/{}'.format(
         self.building1.building_id),
                        content_type='text/html',
                        data='This is not json')
     self.assertEqual(raw.status_code, 400)
Esempio n. 16
0
 def test_edit_protocol_family_no_family_name(self):
     db_session.add(self.family1)
     db_session.commit()
     q_str = '/api/protocol-family/{}'.format(self.family1.family_id)
     raw = self.app.put(q_str,
                        content_type='application/json',
                        data=json.dumps({}))
     self.assertEqual(raw.status_code, 400)
Esempio n. 17
0
 def test_put_protocol_no_value_specified(self):
     db_session.add(self.protocol1)
     db_session.commit()
     raw = self.app.put('/api/protocol/{}'.format(
         self.protocol1.protocol_id),
                        content_type='application/json',
                        data='{}')
     self.assertEqual(raw.status_code, 400)
Esempio n. 18
0
 def test_put_protocol_not_json(self):
     db_session.add(self.protocol1)
     db_session.commit()
     raw = self.app.put('/api/protocol/{}'.format(
         self.protocol1.protocol_id),
                        content_type='text/html',
                        data='This is not JSON')
     self.assertEqual(raw.status_code, 400)
Esempio n. 19
0
 def test_delete_part(self):
     db_session.add(self.part1)
     db_session.commit()
     part_id = self.part1.part_id
     raw = self.app.delete('/api/part/{}'.format(self.part1.element_id))
     deleted = Part.query.first()
     self.assertTrue(deleted is None)
     self.assertEquals({'part_id': part_id}, json.loads(raw.data))
Esempio n. 20
0
 def test_put_not_json_fails(self):
     db_session.add(self.part1)
     db_session.commit()
     to_put = self.part1.to_json()
     raw = self.app.put('/api/part/{}'.format(self.part1.element_id),
                        content_type='text/html',
                        data='This is not json')
     self.assertEqual(400, raw.status_code)
Esempio n. 21
0
 def test_put_building_no_name(self):
     db_session.add(self.building1)
     db_session.commit()
     raw = self.app.put('/api/building/{}'.format(
         self.building1.building_id),
                        content_type='application/json',
                        data='{}')
     self.assertEqual(raw.status_code, 400)
Esempio n. 22
0
 def test_connect_part_protocol_nonexistent_protocol_fails(self):
     part = Part(element_id=1234)
     db_session.add(part)
     db_session.commit()
     raw = self.app.post('/api/part/{}/protocol/{}'.format(
         part.element_id, 100))
     resp = json.loads(raw.data)
     self.assertEqual(raw.status_code, 404)
     self.assertTrue(resp['Error'].startswith('Protocol'))
Esempio n. 23
0
 def test_connect_part_protocol_nonexistent_part_fails(self):
     protocol = Protocol(value='Turn off the lights when leaving')
     db_session.add(protocol)
     db_session.commit()
     raw = self.app.post('/api/part/{}/protocol/{}'.format(
         100, protocol.protocol_id))
     self.assertEqual(raw.status_code, 404)
     resp = json.loads(raw.data)
     self.assertTrue(resp['Error'].startswith('Part'))
Esempio n. 24
0
 def test_add_family_protocol_association_successful(self):
     db_session.add_all([self.family1, self.protocol1])
     db_session.commit()
     q_str = '/api/protocol-family/{}/protocol/{}'.format(
         self.family1.family_id, self.protocol1.protocol_id)
     self.app.post(q_str)
     pfp = ProtocolFamilyProtocol.query.first()
     self.assertEqual(pfp.family_id, self.family1.family_id)
     self.assertEqual(pfp.protocol_id, self.protocol1.protocol_id)
Esempio n. 25
0
 def test_connect_part_protocol(self):
     part = Part(element_id=1234)
     protocol = Protocol(value='Turn off the lights when leaving')
     db_session.add_all([part, protocol])
     db_session.commit()
     raw = self.app.post('/api/part/{}/protocol/{}'.format(
         part.element_id, protocol.protocol_id))
     resp = json.loads(raw.data)
     added = PartProtocol.query.first()
     self.assertEqual(resp, added.to_json())
Esempio n. 26
0
 def test_put_part_extra_keys_fails(self):
     db_session.add(self.part1)
     db_session.commit()
     to_put = self.part1.to_json()
     to_put['element_id'] = 111
     to_put['not_an_attr'] = 'something'
     raw = self.app.put('/api/part/{}'.format(self.part1.element_id),
                        content_type='application/json',
                        data=json.dumps(to_put))
     self.assertEqual(400, raw.status_code)
Esempio n. 27
0
 def test_put_protocol(self):
     db_session.add(self.protocol1)
     db_session.commit()
     raw = self.app.put('/api/protocol/{}'.format(
         self.protocol1.protocol_id),
                        content_type='application/json',
                        data=json.dumps({'value': 'different'}))
     changed = Protocol.query.filter(
         Protocol.protocol_id == self.protocol1.protocol_id).first()
     self.assertEqual(changed.value, 'different')
Esempio n. 28
0
 def test_edit_protocol_family_successful(self):
     fam = ProtocolFamily(family_name='Testfam', family_id=0)
     db_session.add(fam)
     db_session.commit()
     q_str = '/api/protocol-family/0'
     self.app.put(q_str,
                  content_type='application/json',
                  data=json.dumps({'family_name': 'super family1'}))
     changed = ProtocolFamily.query.first()
     self.assertEqual(changed.to_json()['family_name'], 'super family1')
Esempio n. 29
0
 def test_put_protocol_extra_keys_specified(self):
     db_session.add(self.protocol1)
     db_session.commit()
     raw = self.app.put('/api/protocol/{}'.format(
         self.protocol1.protocol_id),
                        content_type='application/json',
                        data=json.dumps({
                            'value': 'something',
                            'else': 'no good'
                        }))
     self.assertEqual(raw.status_code, 400)
Esempio n. 30
0
 def test_put_building_extra_keys(self):
     db_session.add(self.building1)
     db_session.commit()
     raw = self.app.put('/api/building/{}'.format(
         self.building1.building_id),
                        content_type='application/json',
                        data=json.dumps({
                            'name': 'Better Shelby',
                            'something': 'else'
                        }))
     self.assertEqual(raw.status_code, 400)