|
6 | 6 | from sqlalchemy import and_, select |
7 | 7 |
|
8 | 8 | from server.config import config |
9 | | -from server.db.models import avatars, avatars_list, ban |
| 9 | +from server.db.models import avatars, avatars_list, ban, friends_and_foes |
10 | 10 | from server.protocol import DisconnectedError |
11 | 11 | from tests.utils import fast_forward |
12 | 12 |
|
@@ -996,3 +996,215 @@ async def test_avatar_select_not_owned(lobby_server, database): |
996 | 996 | async with database.acquire() as conn: |
997 | 997 | result = await get_player_selected_avatars(conn, player_id) |
998 | 998 | assert result.rowcount == 0 |
| 999 | + |
| 1000 | + |
| 1001 | +@fast_forward(30) |
| 1002 | +async def test_social_add_friend(lobby_server, database): |
| 1003 | + subject_id = 10 |
| 1004 | + player_id, _, proto = await connect_and_sign_in( |
| 1005 | + ("test", "test_password"), |
| 1006 | + lobby_server, |
| 1007 | + ) |
| 1008 | + await read_until_command(proto, "game_info") |
| 1009 | + |
| 1010 | + async with database.acquire() as conn: |
| 1011 | + result = await conn.execute( |
| 1012 | + select(friends_and_foes) |
| 1013 | + .where( |
| 1014 | + and_( |
| 1015 | + friends_and_foes.c.user_id == player_id, |
| 1016 | + friends_and_foes.c.subject_id == subject_id, |
| 1017 | + ) |
| 1018 | + ) |
| 1019 | + ) |
| 1020 | + row = result.fetchone() |
| 1021 | + assert row is None |
| 1022 | + |
| 1023 | + # Other player doesn't even need to be online |
| 1024 | + await proto.send_message({ |
| 1025 | + "command": "social_add", |
| 1026 | + "friend": subject_id, |
| 1027 | + }) |
| 1028 | + await asyncio.sleep(5) |
| 1029 | + |
| 1030 | + async with database.acquire() as conn: |
| 1031 | + result = await conn.execute( |
| 1032 | + select(friends_and_foes) |
| 1033 | + .where( |
| 1034 | + and_( |
| 1035 | + friends_and_foes.c.user_id == player_id, |
| 1036 | + friends_and_foes.c.subject_id == subject_id, |
| 1037 | + ) |
| 1038 | + ) |
| 1039 | + ) |
| 1040 | + row = result.fetchone() |
| 1041 | + |
| 1042 | + assert row.subject_id == subject_id |
| 1043 | + assert row.status == "FRIEND" |
| 1044 | + |
| 1045 | + |
| 1046 | +@fast_forward(30) |
| 1047 | +async def test_social_add_foe(lobby_server, database): |
| 1048 | + subject_id = 10 |
| 1049 | + player_id, _, proto = await connect_and_sign_in( |
| 1050 | + ("test", "test_password"), |
| 1051 | + lobby_server, |
| 1052 | + ) |
| 1053 | + await read_until_command(proto, "game_info") |
| 1054 | + |
| 1055 | + async with database.acquire() as conn: |
| 1056 | + result = await conn.execute( |
| 1057 | + select(friends_and_foes) |
| 1058 | + .where( |
| 1059 | + and_( |
| 1060 | + friends_and_foes.c.user_id == player_id, |
| 1061 | + friends_and_foes.c.subject_id == subject_id, |
| 1062 | + ) |
| 1063 | + ) |
| 1064 | + ) |
| 1065 | + row = result.fetchone() |
| 1066 | + assert row is None |
| 1067 | + |
| 1068 | + # Other player doesn't even need to be online |
| 1069 | + await proto.send_message({ |
| 1070 | + "command": "social_add", |
| 1071 | + "foe": subject_id, |
| 1072 | + }) |
| 1073 | + await asyncio.sleep(5) |
| 1074 | + |
| 1075 | + async with database.acquire() as conn: |
| 1076 | + result = await conn.execute( |
| 1077 | + select(friends_and_foes) |
| 1078 | + .where( |
| 1079 | + and_( |
| 1080 | + friends_and_foes.c.user_id == player_id, |
| 1081 | + friends_and_foes.c.subject_id == subject_id, |
| 1082 | + ) |
| 1083 | + ) |
| 1084 | + ) |
| 1085 | + row = result.fetchone() |
| 1086 | + |
| 1087 | + assert row.subject_id == subject_id |
| 1088 | + assert row.status == "FOE" |
| 1089 | + |
| 1090 | + |
| 1091 | +@fast_forward(30) |
| 1092 | +async def test_social_add_friend_while_hosting(lobby_server): |
| 1093 | + _, _, proto1 = await connect_and_sign_in( |
| 1094 | + ("test", "test_password"), |
| 1095 | + lobby_server, |
| 1096 | + ) |
| 1097 | + rhiza_id, _, proto2 = await connect_and_sign_in( |
| 1098 | + ("Rhiza", "puff_the_magic_dragon"), |
| 1099 | + lobby_server, |
| 1100 | + ) |
| 1101 | + await read_until_command(proto1, "game_info") |
| 1102 | + await read_until_command(proto2, "game_info") |
| 1103 | + |
| 1104 | + game_id = await host_game(proto1, visibility="friends") |
| 1105 | + with pytest.raises(asyncio.TimeoutError): |
| 1106 | + await read_until_command(proto2, "game_info", timeout=10, uid=game_id) |
| 1107 | + |
| 1108 | + await proto1.send_message({ |
| 1109 | + "command": "social_add", |
| 1110 | + "friend": rhiza_id, |
| 1111 | + }) |
| 1112 | + |
| 1113 | + await read_until_command( |
| 1114 | + proto2, |
| 1115 | + "game_info", |
| 1116 | + timeout=10, |
| 1117 | + uid=game_id, |
| 1118 | + state="open", |
| 1119 | + ) |
| 1120 | + |
| 1121 | + |
| 1122 | +@fast_forward(30) |
| 1123 | +async def test_social_add_foe_while_hosting(lobby_server): |
| 1124 | + _, _, proto1 = await connect_and_sign_in( |
| 1125 | + ("test", "test_password"), |
| 1126 | + lobby_server, |
| 1127 | + ) |
| 1128 | + rhiza_id, _, proto2 = await connect_and_sign_in( |
| 1129 | + ("Rhiza", "puff_the_magic_dragon"), |
| 1130 | + lobby_server, |
| 1131 | + ) |
| 1132 | + await read_until_command(proto1, "game_info") |
| 1133 | + await read_until_command(proto2, "game_info") |
| 1134 | + |
| 1135 | + game_id = await host_game(proto1) |
| 1136 | + await read_until_command(proto2, "game_info", timeout=10, uid=game_id) |
| 1137 | + |
| 1138 | + await proto1.send_message({ |
| 1139 | + "command": "social_add", |
| 1140 | + "foe": rhiza_id, |
| 1141 | + }) |
| 1142 | + |
| 1143 | + await read_until_command( |
| 1144 | + proto2, |
| 1145 | + "game_info", |
| 1146 | + timeout=10, |
| 1147 | + uid=game_id, |
| 1148 | + state="closed", |
| 1149 | + ) |
| 1150 | + |
| 1151 | + |
| 1152 | +@fast_forward(30) |
| 1153 | +async def test_social_remove_friend_while_hosting(lobby_server): |
| 1154 | + _, _, proto1 = await connect_and_sign_in( |
| 1155 | + ("friends", "friends"), |
| 1156 | + lobby_server, |
| 1157 | + ) |
| 1158 | + test_id, _, proto2 = await connect_and_sign_in( |
| 1159 | + ("test", "test_password"), |
| 1160 | + lobby_server, |
| 1161 | + ) |
| 1162 | + await read_until_command(proto1, "game_info") |
| 1163 | + await read_until_command(proto2, "game_info") |
| 1164 | + |
| 1165 | + game_id = await host_game(proto1, visibility="friends") |
| 1166 | + await read_until_command(proto2, "game_info", timeout=5, uid=game_id) |
| 1167 | + |
| 1168 | + await proto1.send_message({ |
| 1169 | + "command": "social_remove", |
| 1170 | + "friend": test_id, |
| 1171 | + }) |
| 1172 | + |
| 1173 | + await read_until_command( |
| 1174 | + proto2, |
| 1175 | + "game_info", |
| 1176 | + timeout=10, |
| 1177 | + uid=game_id, |
| 1178 | + state="closed", |
| 1179 | + ) |
| 1180 | + |
| 1181 | + |
| 1182 | +@fast_forward(30) |
| 1183 | +async def test_social_remove_foe_while_hosting(lobby_server): |
| 1184 | + _, _, proto1 = await connect_and_sign_in( |
| 1185 | + ("test", "test_password"), |
| 1186 | + lobby_server, |
| 1187 | + ) |
| 1188 | + rhiza_id, _, proto2 = await connect_and_sign_in( |
| 1189 | + ("foed_by_test", "foe"), |
| 1190 | + lobby_server, |
| 1191 | + ) |
| 1192 | + await read_until_command(proto1, "game_info") |
| 1193 | + await read_until_command(proto2, "game_info") |
| 1194 | + |
| 1195 | + game_id = await host_game(proto1) |
| 1196 | + with pytest.raises(asyncio.TimeoutError): |
| 1197 | + await read_until_command(proto2, "game_info", timeout=5, uid=game_id) |
| 1198 | + |
| 1199 | + await proto1.send_message({ |
| 1200 | + "command": "social_remove", |
| 1201 | + "foe": rhiza_id, |
| 1202 | + }) |
| 1203 | + |
| 1204 | + await read_until_command( |
| 1205 | + proto2, |
| 1206 | + "game_info", |
| 1207 | + timeout=10, |
| 1208 | + uid=game_id, |
| 1209 | + state="open", |
| 1210 | + ) |
0 commit comments