def test_create_room_invalid_action_type(session, client):
    r = Room('basic room')
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'recognize',
            'target': 'item',
            'name': 'test item 1',
            'who_owns': 'Hudson',
            'optional_fields': {
                'description': 'an item, probably a toy',
                'who_has_current': 'Luca',
                'due_back': '3/14/2020'
            }
        }
    }

    response = client.post(url_for('server.create_item'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})
    assert response.status_code == 400
    assert response.get_json()['status'] == 'error'
    assert response.get_json()['message'] == 'Wrong request data'
def test_create_room_more_info(session, client):
    r = Room('basic room')
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'create',
            'target': 'item',
            'name': 'test item 1',
            'who_owns': 'Hudson',
            'optional_fields': {
                'description': 'an item, probably a toy',
                'who_has_current': 'Luca',
                'due_back': '3/14/2020'
            }
        }
    }

    response = client.post(url_for('server.create_item'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})

    assert response.status_code == 200
    resp_data = response.get_json()
    assert resp_data['status'] == 'success'
    item = Item.query.first()
    assert item.name == 'test item 1'
    assert item.who_owns == 'Hudson'
    assert item.description == 'an item, probably a toy'
    assert item.who_has_current == 'Luca'
    assert item.due_back == datetime.datetime(2020, 3, 14, 0, 0)
def test_create_room_basic_info_only(session, client):
    r = Room('basic room')
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'create',
            'target': 'item',
            'name': 'test item 1',
            'who_owns': 'Hudson',
        }
    }

    response = client.post(url_for('server.create_item'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})

    assert response.status_code == 200

    resp_data = response.get_json()
    assert resp_data['status'] == 'success'
    assert resp_data['message'] == 'Item added to basic room'
    assert Item.query.count() == 1
    assert Item.query.first().name == 'test item 1'
    assert Item.query.first().who_owns == 'Hudson'
示例#4
0
def test_update_item_with_new_vals(session, client):
    r = Room('basic')
    session.add(r)
    session.commit()
    i = Item(r, 'toy trucks', 'hudson')
    session.add(i)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'update',
            'target': 'item',
            'targetId': i.id,
            'dataToUpdate': {
                'who_has_current': 'Luca',
                'how_long_can_borrow': '3 weeks',
                'due_back': datetime.date(2019, 11, 14).__str__(),
            }
        }
    }

    response = client.post(url_for('server.update_record'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})

    assert response.status_code == 200
    item = Item.query.first()
    assert item.who_has_current == 'Luca'
    assert item.how_long_can_borrow == '3 weeks'
    assert item.due_back == datetime.datetime(2019, 11, 14, 0, 0)
def test_create_room_invalid_basic_info(session, client):
    r = Room('basic room')
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'create',
            'target': 'item',
            'name': 't',
            'who_owns': 'Hudson',
            'optional_fields': {}
        }
    }

    response = client.post(url_for('server.create_item'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})
    assert response.status_code == 400
    assert response.get_json()['status'] == 'error'
    error_string = 'name of new item is too short or data is wrong'
    assert response.get_json()['message'] == error_string

    data['name'] = 'backhoe loader'
    data['who_owns']: ''

    response = client.post(url_for('server.create_item'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})
    assert response.status_code == 400
    assert response.get_json()['status'] == 'error'
    assert response.get_json()['message'] == error_string
示例#6
0
def test_update_room_info_with_password(session, client):
    r = Room('basic')
    r.private = True
    r.password = '******'
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '******',
        'action': {
            'type': 'update',
            'target': 'room',
            'targetId': r.id,
            'dataToUpdate': {
                'room_name': 'test2',
                'private': True,
                'password': '******'
            }
        }
    }
    response = client.post(url_for('server.update_record'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})
    assert response.status_code == 200
    room = Room.query.all()[0]
    assert room.room_name == 'test2'
    assert room.password == 'newpassword'
示例#7
0
def test_simple_update_room_record(session, client):
    r = Room('basic room')
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'update',
            'target': 'room',
            'targetId': r.id,
            'dataToUpdate': {
                'room_name': 'did it change'
            }
        }
    }
    response = client.post(url_for('server.update_record'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})
    assert response == 200

    assert response.get_json()['status'] == 'success'
    assert Room.query.first().room_name == 'did it change'
    assert response.json['dbresponse'] == 1
示例#8
0
def test_update_mutiple_attrs_room(session, client):
    r = Room('basic')
    session.add(r)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'update',
            'target': 'room',
            'targetId': r.id,
            'dataToUpdate': {
                'room_name': 'test2',
                'private': True,
                'password': '******'
            }
        }
    }
    response = client.post(url_for('server.update_record'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})

    assert response == 200
    target_room = Room.query.first()
    assert target_room.room_name == 'test2'
    assert target_room.private == True
    assert target_room.password == 'testpassword'
示例#9
0
def create_basic_room(session, room_name='basic room', private=False):
    new_room = Room(room_name)
    session.add(new_room)
    session.commit()
    if private:
        new_room.private = True
        new_room.password = '******'
        session.commit()
    return new_room
def test_create_room_already_exists(client, session):
    newRoom = Room('new Room')
    session.add(newRoom)
    session.commit()

    post_data = {
        'roomName': 'new Room',
        'private': False,
        'password': None
    }
    response = client.post(
        url_for('server.create_table'),
        data=json.dumps(post_data),
        headers={'Content-Type': 'application/json'}
    )
    assert response.status_code == 200
    response_data = response.json

    assert response_data['url'] != newRoom.encoded_room_name
    assert response_data['status'] == 'success'
示例#11
0
def add_basic_room_item_and_borrowhistory(session):
    r = Room('basic')
    session.add(r)
    session.commit()
    i = Item(r, 'toy trucks', 'hudson')
    session.add(i)
    session.commit()
    bh = BorrowHistory(i, 'Luca')
    session.add(bh)
    session.commit()
    return (r, i, bh)
示例#12
0
def test_update_borrow_history(session, client):
    r = Room('basic')
    session.add(r)
    session.commit()
    i = Item(r, 'toy trucks', 'hudson')
    session.add(i)
    session.commit()
    bh = BorrowHistory(i, 'Luca')
    session.add(bh)
    session.commit()

    data = {
        'requestedRoom': r.encoded_room_name,
        'password': '',
        'action': {
            'type': 'update',
            'target': 'borrowHistory',
            'targetId': bh.id,
            'dataToUpdate': {
                'date_borrowed': datetime.date(2019, 11, 14).__str__(),
                'due_back': datetime.date(2019, 11, 18).__str__(),
                'notes': 'yuck, it smelled funny'
            }
        }
    }

    response = client.post(url_for('server.update_record'),
                           data=json.dumps(data),
                           headers={'Content-Type': 'application/json'})

    assert response.status_code == 200
    assert response.json['status'] == 'success'
    assert response.json['dbresponse'] == 1
    bhist = BorrowHistory.query.first()
    assert bhist.date_borrowed == datetime.datetime(2019, 11, 14, 0, 0)
    assert bhist.notes == 'yuck, it smelled funny'
    assert bhist.due_back == datetime.datetime(2019, 11, 18, 0, 0)
def add_history_to_item(session, item, who_borrowed='Luca'):
    new_history = BorrowHistory(item, who_borrowed)
    session.add(new_history)
    session.commit()
    return new_history
def add_item_to_room(session, room, name='truck toy', who_owns='hudson'):
    new_item = Item(room, name, who_owns)
    session.add(new_item)
    session.commit()
    return new_item