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"
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"
def test_delete_shift(client): congregation = Congregation.objects().first() cong_id = str(congregation.id) shift = Shift.objects().first() assert len(Shift.objects()) == 1 shift_id = str(shift.id) response = client.delete(f'/congregations/{cong_id}/shifts/{shift_id}') assert response.status_code == 200 assert len(Shift.objects()) == 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", start_time=datetime(2021, 1, 1, 14, 0), end_time=datetime(2021, 1, 1, 15, 30), congregation=congregation.to_dbref()).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_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"
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")
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
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
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}'})
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
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"
def set_congregation(id): congregation = Congregation.objects().get(id=id) return congregation
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")