def test_VerifyPhone(): user, token = generate_user() user_id = user.id with account_session(token) as account, api_session(token) as api: with pytest.raises(grpc.RpcError) as e: account.VerifyPhone(account_pb2.VerifyPhoneReq(token="123455")) assert e.value.code() == grpc.StatusCode.FAILED_PRECONDITION res = api.GetUser(api_pb2.GetUserReq(user=str(user_id))) assert res.verification == 0.0 with session_scope() as session: user = session.execute( select(User).where(User.id == user_id)).scalar_one() user.phone = "+46701740605" user.phone_verification_token = "111112" user.phone_verification_sent = now() account.VerifyPhone(account_pb2.VerifyPhoneReq(token="111112")) res = api.GetUser(api_pb2.GetUserReq(user=str(user_id))) assert res.verification == 1.0 # Phone number should finally show up on in your profile settings res = account.GetAccountInfo(empty_pb2.Empty()) assert res.phone == "+46701740605"
def test_get_user(db): user1, token1 = generate_user() user2, token2 = generate_user() with api_session(token1) as api: res = api.GetUser(api_pb2.GetUserReq(user=user2.username)) assert res.user_id == user2.id assert res.username == user2.username assert res.name == user2.name with api_session(token1) as api: res = api.GetUser(api_pb2.GetUserReq(user=str(user2.id))) assert res.user_id == user2.id assert res.username == user2.username assert res.name == user2.name
def test_invalid_token(db): user1, token1 = generate_user() user2, token2 = generate_user() wrong_token = random_hex(32) with real_api_session(wrong_token) as api, pytest.raises( grpc.RpcError) as e: res = api.GetUser(api_pb2.GetUserReq(user=user2.username)) assert e.value.code() == grpc.StatusCode.UNAUTHENTICATED assert e.value.details() == "Unauthorized"
def test_signup_incremental(db): with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq(basic=auth_pb2.SignupBasic( name="testing", email="*****@*****.**"), )) flow_token = res.flow_token assert res.flow_token assert not res.HasField("auth_res") assert not res.need_basic assert res.need_account assert res.need_feedback assert res.need_verify_email assert res.need_accept_community_guidelines # read out the signup token directly from the database for now with session_scope() as session: flow = session.execute( select(SignupFlow).where( SignupFlow.flow_token == flow_token)).scalar_one() assert flow.email_sent assert not flow.email_verified email_token = flow.email_token with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq(flow_token=flow_token)) assert res.flow_token == flow_token assert not res.HasField("auth_res") assert not res.need_basic assert res.need_account assert res.need_feedback assert res.need_verify_email assert res.need_accept_community_guidelines # Add feedback with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq( flow_token=flow_token, feedback=auth_pb2.ContributorForm( ideas="I'm a robot, incapable of original ideation", features="I love all your features", experience="I haven't done couch surfing before", contribute=auth_pb2.CONTRIBUTE_OPTION_YES, contribute_ways=["serving", "backend"], expertise= "I'd love to be your server: I can compute very fast, but only simple opcodes", ), )) assert res.flow_token == flow_token assert not res.HasField("auth_res") assert not res.need_basic assert res.need_account assert not res.need_feedback assert res.need_verify_email assert res.need_accept_community_guidelines # Agree to community guidelines with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq( flow_token=flow_token, accept_community_guidelines=wrappers_pb2.BoolValue(value=True), )) assert res.flow_token == flow_token assert not res.HasField("auth_res") assert not res.need_basic assert res.need_account assert not res.need_feedback assert res.need_verify_email assert not res.need_accept_community_guidelines # Verify email with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq( flow_token=flow_token, email_token=email_token, )) assert res.flow_token == flow_token assert not res.HasField("auth_res") assert not res.need_basic assert res.need_account assert not res.need_feedback assert not res.need_verify_email assert not res.need_accept_community_guidelines # Finally finish off account info with auth_api_session() as (auth_api, metadata_interceptor): res = auth_api.SignupFlow( auth_pb2.SignupFlowReq( flow_token=flow_token, account=auth_pb2.SignupAccount( username="******", password="******", birthdate="1970-01-01", gender="Bot", hosting_status=api_pb2.HOSTING_STATUS_MAYBE, city="New York City", lat=40.7331, lng=-73.9778, radius=500, accept_tos=True, ), )) assert not res.flow_token assert res.HasField("auth_res") assert res.auth_res.user_id assert not res.auth_res.jailed assert not res.need_basic assert not res.need_account assert not res.need_feedback assert not res.need_verify_email assert not res.need_accept_community_guidelines user_id = res.auth_res.user_id sess_token = get_session_cookie_token(metadata_interceptor) with api_session(sess_token) as api: res = api.GetUser(api_pb2.GetUserReq(user=str(user_id))) assert res.username == "frodo" assert res.gender == "Bot" assert res.hosting_status == api_pb2.HOSTING_STATUS_MAYBE assert res.city == "New York City" assert res.lat == 40.7331 assert res.lng == -73.9778 assert res.radius == 500 with session_scope() as session: form = session.execute(select(ContributorForm)).scalar_one() assert form.ideas == "I'm a robot, incapable of original ideation" assert form.features == "I love all your features" assert form.experience == "I haven't done couch surfing before" assert form.contribute == ContributeOption.yes assert form.contribute_ways == ["serving", "backend"] assert form.expertise == "I'd love to be your server: I can compute very fast, but only simple opcodes"
def test_hosting_preferences(db): user1, token1 = generate_user() user2, token2 = generate_user() with api_session(token1) as api: res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert not res.HasField("max_guests") assert not res.HasField("last_minute") assert not res.HasField("has_pets") assert not res.HasField("accepts_pets") assert not res.HasField("pet_details") assert not res.HasField("has_kids") assert not res.HasField("accepts_kids") assert not res.HasField("kid_details") assert not res.HasField("has_housemates") assert not res.HasField("housemate_details") assert not res.HasField("wheelchair_accessible") assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN assert not res.HasField("smokes_at_home") assert not res.HasField("drinking_allowed") assert not res.HasField("drinks_at_home") assert not res.HasField("other_host_info") assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN assert not res.HasField("sleeping_details") assert not res.HasField("area") assert not res.HasField("house_rules") assert not res.HasField("parking") assert res.parking_details == api_pb2.PARKING_DETAILS_UNKNOWN assert not res.HasField("camping_ok") api.UpdateProfile( api_pb2.UpdateProfileReq( max_guests=api_pb2.NullableUInt32Value(value=3), last_minute=api_pb2.NullableBoolValue(value=True), has_pets=api_pb2.NullableBoolValue(value=False), accepts_pets=api_pb2.NullableBoolValue(value=True), pet_details=api_pb2.NullableStringValue(value="I love dogs"), has_kids=api_pb2.NullableBoolValue(value=False), accepts_kids=api_pb2.NullableBoolValue(value=True), kid_details=api_pb2.NullableStringValue(value="I hate kids"), has_housemates=api_pb2.NullableBoolValue(value=False), housemate_details=api_pb2.NullableStringValue( value="I have 7 housemates"), wheelchair_accessible=api_pb2.NullableBoolValue(value=True), smoking_allowed=api_pb2.SMOKING_LOCATION_WINDOW, area=api_pb2.NullableStringValue(value="area!"), smokes_at_home=api_pb2.NullableBoolValue(value=False), drinking_allowed=api_pb2.NullableBoolValue(value=True), drinks_at_home=api_pb2.NullableBoolValue(value=False), other_host_info=api_pb2.NullableStringValue( value="I'm pretty swell"), sleeping_arrangement=api_pb2.SLEEPING_ARRANGEMENT_COMMON, sleeping_details=api_pb2.NullableStringValue( value="Couch in living room"), house_rules=api_pb2.NullableStringValue(value="RULES!"), parking=api_pb2.NullableBoolValue(value=True), parking_details=api_pb2.PARKING_DETAILS_PAID_ONSITE, camping_ok=api_pb2.NullableBoolValue(value=False), )) # Use a second user to view the hosting preferences just to check # that it is public information. with api_session(token2) as api: res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert res.max_guests.value == 3 assert res.last_minute.value assert not res.has_pets.value assert res.accepts_pets.value assert res.pet_details.value == "I love dogs" assert not res.has_kids.value assert res.accepts_kids.value assert res.kid_details.value == "I hate kids" assert not res.has_housemates.value assert res.housemate_details.value == "I have 7 housemates" assert res.wheelchair_accessible.value assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_WINDOW assert not res.smokes_at_home.value assert res.drinking_allowed.value assert not res.drinks_at_home.value assert res.other_host_info.value == "I'm pretty swell" assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_COMMON assert res.sleeping_details.value == "Couch in living room" assert res.area.value == "area!" assert res.house_rules.value == "RULES!" assert res.parking.value assert res.parking_details == api_pb2.PARKING_DETAILS_PAID_ONSITE assert not res.camping_ok.value # test unsetting with api_session(token1) as api: api.UpdateProfile( api_pb2.UpdateProfileReq( max_guests=api_pb2.NullableUInt32Value(is_null=True), last_minute=api_pb2.NullableBoolValue(is_null=True), has_pets=api_pb2.NullableBoolValue(is_null=True), accepts_pets=api_pb2.NullableBoolValue(is_null=True), pet_details=api_pb2.NullableStringValue(is_null=True), has_kids=api_pb2.NullableBoolValue(is_null=True), accepts_kids=api_pb2.NullableBoolValue(is_null=True), kid_details=api_pb2.NullableStringValue(is_null=True), has_housemates=api_pb2.NullableBoolValue(is_null=True), housemate_details=api_pb2.NullableStringValue(is_null=True), wheelchair_accessible=api_pb2.NullableBoolValue(is_null=True), smoking_allowed=api_pb2.SMOKING_LOCATION_UNKNOWN, area=api_pb2.NullableStringValue(is_null=True), smokes_at_home=api_pb2.NullableBoolValue(is_null=True), drinking_allowed=api_pb2.NullableBoolValue(is_null=True), drinks_at_home=api_pb2.NullableBoolValue(is_null=True), other_host_info=api_pb2.NullableStringValue(is_null=True), sleeping_arrangement=api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN, sleeping_details=api_pb2.NullableStringValue(is_null=True), house_rules=api_pb2.NullableStringValue(is_null=True), parking=api_pb2.NullableBoolValue(is_null=True), parking_details=api_pb2.PARKING_DETAILS_UNKNOWN, camping_ok=api_pb2.NullableBoolValue(is_null=True), )) res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert not res.HasField("max_guests") assert not res.HasField("last_minute") assert not res.HasField("has_pets") assert not res.HasField("accepts_pets") assert not res.HasField("pet_details") assert not res.HasField("has_kids") assert not res.HasField("accepts_kids") assert not res.HasField("kid_details") assert not res.HasField("has_housemates") assert not res.HasField("housemate_details") assert not res.HasField("wheelchair_accessible") assert res.smoking_allowed == api_pb2.SMOKING_LOCATION_UNKNOWN assert not res.HasField("smokes_at_home") assert not res.HasField("drinking_allowed") assert not res.HasField("drinks_at_home") assert not res.HasField("other_host_info") assert res.sleeping_arrangement == api_pb2.SLEEPING_ARRANGEMENT_UNKNOWN assert not res.HasField("sleeping_details") assert not res.HasField("area") assert not res.HasField("house_rules") assert not res.HasField("parking") assert res.parking_details == api_pb2.PARKING_DETAILS_UNKNOWN assert not res.HasField("camping_ok")
def test_coords(db): # make them have not added a location user1, token1 = generate_user(geom=None, geom_radius=None) user2, token2 = generate_user() with api_session(token2) as api: res = api.Ping(api_pb2.PingReq()) assert res.user.city == user2.city lat, lng = user2.coordinates or (0, 0) assert res.user.lat == lat assert res.user.lng == lng assert res.user.radius == user2.geom_radius with api_session(token2) as api: res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert res.city == user1.city assert res.lat == 0.0 assert res.lng == 0.0 assert res.radius == 0.0 # Check coordinate wrapping user3, token3 = generate_user(geom=create_coordinate(40.0, -180.5)) user4, token4 = generate_user(geom=create_coordinate(40.0, 20.0)) user5, token5 = generate_user(geom=create_coordinate(90.5, 20.0)) with api_session(token3) as api: res = api.GetUser(api_pb2.GetUserReq(user=user3.username)) assert res.lat == 40.0 assert res.lng == 179.5 with api_session(token4) as api: res = api.GetUser(api_pb2.GetUserReq(user=user4.username)) assert res.lat == 40.0 assert res.lng == 20.0 # PostGIS does not wrap longitude for latitude overflow with api_session(token5) as api: res = api.GetUser(api_pb2.GetUserReq(user=user5.username)) assert res.lat == 89.5 assert res.lng == 20.0 with real_jail_session(token1) as jail: res = jail.JailInfo(empty_pb2.Empty()) assert res.jailed assert res.has_not_added_location res = jail.SetLocation( jail_pb2.SetLocationReq( city="New York City", lat=40.7812, lng=-73.9647, radius=250, )) assert not res.jailed assert not res.has_not_added_location res = jail.JailInfo(empty_pb2.Empty()) assert not res.jailed assert not res.has_not_added_location with api_session(token2) as api: res = api.GetUser(api_pb2.GetUserReq(user=user1.username)) assert res.city == "New York City" assert res.lat == 40.7812 assert res.lng == -73.9647 assert res.radius == 250
def test_language_abilities(db): user, token = generate_user() with api_session(token) as api: res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 2 # can't add non-existent languages with pytest.raises(grpc.RpcError) as e: res = api.UpdateProfile( api_pb2.UpdateProfileReq( language_abilities=api_pb2.RepeatedLanguageAbilityValue( value=[ api_pb2.LanguageAbility( code="QQQ", fluency=api_pb2.LanguageAbility.Fluency. FLUENCY_FLUENT, ) ], ), )) assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT assert e.value.details() == errors.INVALID_LANGUAGE # can't have multiple languages of the same type with pytest.raises(Exception) as e: res = api.UpdateProfile( api_pb2.UpdateProfileReq( language_abilities=api_pb2.RepeatedLanguageAbilityValue( value=[ api_pb2.LanguageAbility( code="eng", fluency=api_pb2.LanguageAbility.Fluency. FLUENCY_FLUENT, ), api_pb2.LanguageAbility( code="eng", fluency=api_pb2.LanguageAbility.Fluency. FLUENCY_FLUENT, ), ], ), )) assert "violates unique constraint" in str(e.value) # nothing changed res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 2 # now actually add a value api.UpdateProfile( api_pb2.UpdateProfileReq( language_abilities=api_pb2.RepeatedLanguageAbilityValue(value=[ api_pb2.LanguageAbility( code="eng", fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT, ) ], ), )) res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 1 assert res.language_abilities[0].code == "eng" assert res.language_abilities[ 0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT # change the value to a new one api.UpdateProfile( api_pb2.UpdateProfileReq( language_abilities=api_pb2.RepeatedLanguageAbilityValue(value=[ api_pb2.LanguageAbility( code="fin", fluency=api_pb2.LanguageAbility.Fluency. FLUENCY_BEGINNER, ) ], ), )) res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 1 assert res.language_abilities[0].code == "fin" assert res.language_abilities[ 0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER # should be able to set to same value still api.UpdateProfile( api_pb2.UpdateProfileReq( language_abilities=api_pb2.RepeatedLanguageAbilityValue(value=[ api_pb2.LanguageAbility( code="fin", fluency=api_pb2.LanguageAbility.Fluency. FLUENCY_BEGINNER, ) ], ), )) res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 1 assert res.language_abilities[0].code == "fin" assert res.language_abilities[ 0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER # don't change it api.UpdateProfile(api_pb2.UpdateProfileReq()) res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 1 assert res.language_abilities[0].code == "fin" assert res.language_abilities[ 0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_BEGINNER # remove value api.UpdateProfile( api_pb2.UpdateProfileReq( language_abilities=api_pb2.RepeatedLanguageAbilityValue( value=[], ), )) res = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert len(res.language_abilities) == 0
def test_update_profile(db): user, token = generate_user() with api_session(token) as api: with pytest.raises(grpc.RpcError) as e: api.UpdateProfile( api_pb2.UpdateProfileReq(name=wrappers_pb2.StringValue( value=" "))) assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT assert e.value.details() == errors.INVALID_NAME with pytest.raises(grpc.RpcError) as e: api.UpdateProfile( api_pb2.UpdateProfileReq( lat=wrappers_pb2.DoubleValue(value=0), lng=wrappers_pb2.DoubleValue(value=0))) assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT assert e.value.details() == errors.INVALID_COORDINATE with pytest.raises(grpc.RpcError) as e: api.UpdateProfile( api_pb2.UpdateProfileReq( regions_visited=api_pb2.RepeatedStringValue( value=["United States"]))) assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT assert e.value.details() == errors.INVALID_REGION with pytest.raises(grpc.RpcError) as e: api.UpdateProfile( api_pb2.UpdateProfileReq( regions_lived=api_pb2.RepeatedStringValue( value=["United Kingdom"]))) assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT assert e.value.details() == errors.INVALID_REGION api.UpdateProfile( api_pb2.UpdateProfileReq( name=wrappers_pb2.StringValue(value="New name"), city=wrappers_pb2.StringValue(value="Timbuktu"), hometown=api_pb2.NullableStringValue(value="Walla Walla"), lat=wrappers_pb2.DoubleValue(value=0.01), lng=wrappers_pb2.DoubleValue(value=-2), radius=wrappers_pb2.DoubleValue(value=321), pronouns=api_pb2.NullableStringValue(value="Ro, Robo, Robots"), occupation=api_pb2.NullableStringValue(value="Testing"), education=api_pb2.NullableStringValue(value="Couchers U"), about_me=api_pb2.NullableStringValue(value="I rule"), my_travels=api_pb2.NullableStringValue( value="Oh the places you'll go!"), things_i_like=api_pb2.NullableStringValue(value="Couchers"), about_place=api_pb2.NullableStringValue(value="My place"), hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST, meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP, language_abilities=api_pb2.RepeatedLanguageAbilityValue(value=[ api_pb2.LanguageAbility( code="eng", fluency=api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT, ) ], ), regions_visited=api_pb2.RepeatedStringValue( value=["CXR", "FIN"]), regions_lived=api_pb2.RepeatedStringValue( value=["USA", "EST"]), additional_information=api_pb2.NullableStringValue( value="I <3 Couchers"), )) user_details = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert user_details.name == "New name" assert user_details.city == "Timbuktu" assert user_details.hometown == "Walla Walla" assert user_details.pronouns == "Ro, Robo, Robots" assert user_details.education == "Couchers U" assert user_details.my_travels == "Oh the places you'll go!" assert user_details.things_i_like == "Couchers" assert user_details.lat == 0.01 assert user_details.lng == -2 assert user_details.radius == 321 assert user_details.occupation == "Testing" assert user_details.about_me == "I rule" assert user_details.about_place == "My place" assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP assert user_details.language_abilities[0].code == "eng" assert user_details.language_abilities[ 0].fluency == api_pb2.LanguageAbility.Fluency.FLUENCY_FLUENT assert user_details.additional_information == "I <3 Couchers" assert user_details.regions_visited == ["CXR", "FIN"] assert user_details.regions_lived == ["EST", "USA"] # Test unset values api.UpdateProfile( api_pb2.UpdateProfileReq( hometown=api_pb2.NullableStringValue(is_null=True), radius=wrappers_pb2.DoubleValue(value=0), pronouns=api_pb2.NullableStringValue(is_null=True), occupation=api_pb2.NullableStringValue(is_null=True), education=api_pb2.NullableStringValue(is_null=True), about_me=api_pb2.NullableStringValue(is_null=True), my_travels=api_pb2.NullableStringValue(is_null=True), things_i_like=api_pb2.NullableStringValue(is_null=True), about_place=api_pb2.NullableStringValue(is_null=True), hosting_status=api_pb2.HOSTING_STATUS_CAN_HOST, meetup_status=api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP, language_abilities=api_pb2.RepeatedLanguageAbilityValue( value=[]), regions_visited=api_pb2.RepeatedStringValue(value=[]), regions_lived=api_pb2.RepeatedStringValue(value=[]), additional_information=api_pb2.NullableStringValue( is_null=True), )) user_details = api.GetUser(api_pb2.GetUserReq(user=user.username)) assert not user_details.hometown assert not user_details.radius assert not user_details.pronouns assert not user_details.occupation assert not user_details.education assert not user_details.about_me assert not user_details.my_travels assert not user_details.things_i_like assert not user_details.about_place assert user_details.hosting_status == api_pb2.HOSTING_STATUS_CAN_HOST assert user_details.meetup_status == api_pb2.MEETUP_STATUS_WANTS_TO_MEETUP assert not user_details.language_abilities assert not user_details.regions_visited assert not user_details.regions_lived assert not user_details.additional_information