Coverage for src/tests/test_blocking.py: 100%
74 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-12-20 18:03 +0000
« prev ^ index » next coverage.py v7.5.0, created at 2024-12-20 18:03 +0000
1import grpc
2import pytest
3from google.protobuf import empty_pb2
5from couchers import errors
6from couchers.models import UserBlock
7from couchers.sql import couchers_select as select
8from proto import blocking_pb2
9from tests.test_fixtures import blocking_session, db, generate_user, make_user_block, session_scope, testconfig # noqa
12@pytest.fixture(autouse=True)
13def _(testconfig):
14 pass
17def test_BlockUser(db):
18 user1, token1 = generate_user()
19 user2, token2 = generate_user()
21 with session_scope() as session:
22 blocked_user_list = (
23 session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
24 )
25 assert len(blocked_user_list) == 0
27 with blocking_session(token1) as user_blocks:
28 with pytest.raises(grpc.RpcError) as e:
29 user_blocks.BlockUser(blocking_pb2.BlockUserReq(username=user1.username))
30 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
31 assert e.value.details() == errors.CANT_BLOCK_SELF
33 user_blocks.BlockUser(blocking_pb2.BlockUserReq(username=user2.username))
35 with pytest.raises(grpc.RpcError) as e:
36 user_blocks.BlockUser(blocking_pb2.BlockUserReq(username=user2.username))
37 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
38 assert e.value.details() == errors.USER_ALREADY_BLOCKED
40 with session_scope() as session:
41 blocked_user_list = (
42 session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
43 )
44 assert len(blocked_user_list) == 1
47def test_make_user_block(db):
48 user1, token1 = generate_user()
49 user2, token2 = generate_user()
51 make_user_block(user1, user2)
53 with session_scope() as session:
54 blocked_user_list = (
55 session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
56 )
57 assert len(blocked_user_list) == 1
60def test_UnblockUser(db):
61 user1, token1 = generate_user()
62 user2, token2 = generate_user()
63 make_user_block(user1, user2)
65 with blocking_session(token1) as user_blocks:
66 user_blocks.UnblockUser(blocking_pb2.UnblockUserReq(username=user2.username))
68 with session_scope() as session:
69 blocked_users = session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
70 assert len(blocked_users) == 0
72 with blocking_session(token1) as user_blocks:
73 with pytest.raises(grpc.RpcError) as e:
74 user_blocks.UnblockUser(blocking_pb2.UnblockUserReq(username=user2.username))
75 assert e.value.code() == grpc.StatusCode.INVALID_ARGUMENT
76 assert e.value.details() == errors.USER_NOT_BLOCKED
78 # Test re-blocking
79 user_blocks.BlockUser(blocking_pb2.BlockUserReq(username=user2.username))
81 with session_scope() as session:
82 blocked_users = session.execute(select(UserBlock).where(UserBlock.blocking_user_id == user1.id)).scalars().all()
83 assert len(blocked_users) == 1
86def test_GetBlockedUsers(db):
87 user1, token1 = generate_user()
88 user2, token2 = generate_user()
89 user3, token3 = generate_user()
91 with blocking_session(token1) as user_blocks:
92 # Check no blocked users to start
93 blocked_user_list = user_blocks.GetBlockedUsers(empty_pb2.Empty())
94 assert len(blocked_user_list.blocked_usernames) == 0
96 make_user_block(user1, user2)
97 make_user_block(user1, user3)
98 blocked_user_list = user_blocks.GetBlockedUsers(empty_pb2.Empty())
99 assert len(blocked_user_list.blocked_usernames) == 2
102def test_relationships_userblock_dot_user(db):
103 user1, token1 = generate_user()
104 user2, token2 = generate_user()
106 make_user_block(user1, user2)
108 with session_scope() as session:
109 block = session.execute(
110 select(UserBlock).where((UserBlock.blocking_user_id == user1.id) & (UserBlock.blocked_user_id == user2.id))
111 ).scalar_one_or_none()
112 assert block.blocking_user.username == user1.username
113 assert block.blocked_user.username == user2.username