Example #1
0
def test_delete_shift(client):
    congregation = Congregation.objects().first()
    cong_id = str(congregation.id)
    assert len(congregation.shifts) == 1
    shift = Shift.objects().first()
    shift_id = str(shift.id)
    response = client.delete(f'/congregations/{cong_id}/shifts/{shift_id}')
    assert response.status_code == 200
    congregation = Congregation.objects().first()
    assert len(congregation.shifts) == 0
    assert len(Shift.objects()) == 0
Example #2
0
def test_put_congregation(client):
    payload = {"name": "Not Willimantic"}
    cong_id = str(Congregation.objects().first().id)
    response = client.put(f'/congregations/{cong_id}', json=payload)
    assert response.status_code == 200
    data = response.get_json()
    assert data["name"] == "Not Willimantic"
Example #3
0
def test_create_congregation(client):
    congregation = Congregation(
        name="Test Congregation"
    )
    assert congregation.name == "Test Congregation"
    congregation.save()
    assert (Congregation.objects().order_by('-id')
            .first().name == "Test Congregation")
Example #4
0
def test_get_shifts(client):
    congregation = Congregation.objects().first()
    cong_id = str(congregation.id)
    response = client.get(f'/congregations/{cong_id}/shifts/')
    assert response.status_code == 200
    data = response.get_json()
    assert len(data) == 1
    assert data[0]["location"] == "UConn"
Example #5
0
def test_put_shift(client):
    congregation = Congregation.objects().first()
    cong_id = str(congregation.id)
    shift = Shift.objects().first()
    shift_id = str(shift.id)
    response = client.put(f'/congregations/{cong_id}/shifts/{shift_id}',
                          json={"location": "Dam trail"})
    assert response.status_code == 200
    data = response.get_json()
    assert data["location"] == "Dam trail"
Example #6
0
def app():
    db = MongoEngine()
    crypt = Bcrypt()
    mongo = PyMongo()
    mail = Mail()

    login_manager = LoginManager()
    login_manager.login_view = None
    login_manager.login_message_category = 'info'

    app = create_app({
        "SECRET_KEY": 'testsecret',
        "SECURITY_PASSWORD_SALT": 'testsalt',
        "SECURITY_CSRF_COOKIE": {
            "key": "XSRF-TOKEN"
        },
        "SECURITY_CSRF_IGNORE_UNAUTH_ENDPOINTS": True,
        "WTF_CSRF_TIME_LIMIT": None,
        "WTF_CSRF_CHECK_DEFAULT": False,
        "MONGODB_SETTINGS": {
            'host': 'mongodb://localhost/pwsched-test'
        },
        "MONGO_URI": 'mongodb://localhost/pwsched-test',
        "TESTING": True
    })

    db.init_app(app)
    crypt.init_app(app)
    login_manager.init_app(app)
    mongo.init_app(app)
    mail.init_app(app)

    Shift.drop_collection()
    Congregation.drop_collection()
    User.drop_collection()

    congregation = Congregation(name="English - Willimantic").save()
    shift = Shift(location="UConn",
                  datetime=datetime.now,
                  congregation=congregation.to_dbref()).save()
    congregation.shifts.append(shift.to_dbref())
    congregation.save()
    hashed_password = crypt.generate_password_hash('password').decode('utf-8')
    User(
        name="Brother Service Overseer",
        email="*****@*****.**",
        password=hashed_password,
        congregation=(
            Congregation.objects().order_by('-id').first().to_dbref())).save()

    yield app

    Shift.drop_collection()
    Congregation.drop_collection()
    User.drop_collection()
def test_create_user(client):
    user = User(name="Spongebob",
                email="*****@*****.**",
                password="******",
                congregation=(
                    Congregation.objects().order_by('-id').first().to_dbref()))
    assert user.email == "*****@*****.**"
    user.save()
    assert (
        User.objects().order_by('-id').first().email == "*****@*****.**")
    assert (User.objects().order_by('-id').first().congregation.name ==
            "English - Willimantic")
Example #8
0
def test_get_shift(client):
    congregation = Congregation.objects().first()
    cong_id = str(congregation.id)
    shift = Shift.objects().first()
    shift_id = str(shift.id)
    response = client.get(f'/congregations/{cong_id}/shifts/{shift_id}')
    assert response.status_code == 200
    # Create a new congregation - should return 401
    # if shift doesn't belong to congregation
    congregation = Congregation(name="sneaky").save()
    cong_id = str(congregation.id)
    response = client.get(f'/congregations/{cong_id}/shifts/{shift_id}')
    assert response.status_code == 401
Example #9
0
def test_post_shifts(client):
    congregation = Congregation.objects().first()
    cong_id = str(congregation.id)
    assert len(congregation.shifts) == 1
    payload = {
        "location": "Dam trail",
        "datetime": "2017-06-01T08:30"
    }
    response = client.post(f'/congregations/{cong_id}/shifts/',
                           json=payload)
    assert response.status_code == 200
    # Check that shift is created in db w appropriate associations
    shift = Shift.objects().order_by('-id').first()
    assert shift.location == "Dam trail"
    assert shift.congregation.name == "English - Willimantic"
    congregation = Congregation.objects().first()
    assert len(congregation.shifts) == 2
    assert congregation.shifts[1].location == "Dam trail"
    # Check that response contains correct shift data
    data = response.get_json()
    assert data["location"] == "Dam trail"
    assert len(data["volunteers"]) == 0
    assert len(data["requested_by"]) == 0
Example #10
0
def test_request_shift(client):
    congregation = Congregation.objects().first()
    cong_id = str(congregation.id)
    shift = Shift.objects().first()
    shift_id = str(shift.id)
    user = User.objects().first()
    user_id = str(user.id)
    response = client.put(
        f'/congregations/{cong_id}/shifts/{shift_id}'
        '/request',
        json={"userId": user_id})
    assert response.status_code == 200
    data = response.get_json()
    assert len(data["requested_by"]) == 1
Example #11
0
def register():
    body = request.get_json()
    email = body["email"]
    password = body["password"]
    name = body["name"]
    hashed_password = (crypt.generate_password_hash(password).decode('utf-8'))
    congregation = Congregation.objects().get(name=body["congregation"])
    user = User(email=email,
                password=hashed_password,
                name=name,
                congregation=congregation.to_dbref())
    user.save()
    login_user(user)
    token = (jwt.encode({
        "email": user.email
    }, os.environ.get("SECRET_KEY")).decode('utf-8'))
    return (jsonify({"user": user}), 200, {"Set-Cookie": f'auth={token}'})
Example #12
0
def test_delete_congregation(client):
    cong_id = str(Congregation.objects().first().id)
    response = client.delete(f'/congregations/{cong_id}')
    assert response.status_code == 200
    assert len(Congregation.objects()) == 0
Example #13
0
def test_get_congregation(client):
    cong_id = str(Congregation.objects().first().id)
    response = client.get(f'/congregations/{cong_id}')
    assert response.status_code == 200
    data = response.get_json()
    assert data["name"] == "English - Willimantic"
Example #14
0
def set_congregation(id):
    congregation = Congregation.objects().get(id=id)
    return congregation