Browse Source

[refactor] User Liste ist nun ein dictionary, kommentare zwecks lesbarkeit wieder entfernt

develop
Wayne Colin Abel 5 years ago
parent
commit
128879ffdd
  1. 13
      src/server.py
  2. 73
      test/test_server.py

13
src/server.py

@ -22,15 +22,14 @@ class RequestHandler(socketserver.BaseRequestHandler):
def command_nickname(self, user, desired_nickname):
if desired_nickname not in self.user_list:
self.user_list.append(desired_nickname)
self.user_list.remove(user)
message = ":" + user + " NICK " + desired_nickname
if desired_nickname not in self.user_dict.keys():
self.user_dict[desired_nickname] = dict()
self.user_dict.pop(user)
return True
else:
self.user_list.remove(desired_nickname)
if user in self.user_list:
self.user_list.remove(user)
self.user_dict.pop(desired_nickname)
if user in self.user_dict:
self.user_dict.pop(user)
# issue kill command (?)
return True

73
test/test_server.py

@ -14,72 +14,29 @@ def test_ping(server):
@pytest.mark.parametrize("test_input,expected", [("password", True), ("wrong password", False)])
def test_command_pass(server, test_input, expected):
"""
Command: PASS
Parameters: <password>
The PASS command is used to set a 'connection password'. The
password can and must be set before any attempt to register the
connection is made. Currently this requires that clients send a PASS
command before sending the NICK/USER combination and servers *must*
send a PASS command before any SERVER command. The password supplied
must match the one contained in the C/N lines (for servers) or I
lines (for clients). It is possible to send multiple PASS commands
before registering but only the last one sent is used for
verification and it may not be changed once registered. Numeric
Replies:
ERR_NEEDMOREPARAMS ERR_ALREADYREGISTRED
Example:
PASS secretpasswordhere
"""
server.password = "password"
assert server.command_pass(server, test_input) == expected
def create_user_list():
user_list = list()
def create_user_dict():
user_dict = dict()
user_id = "user"
for i in range(0, 9):
user_list.append((user_id + str(i)))
user_dict[(user_id + str(i))] = dict()
return user_list
return user_dict
@pytest.mark.parametrize("user_list, user_to_change, nickname, expected_result",
@pytest.mark.parametrize("user_dict, user_to_change, nickname, expected_result",
[
(create_user_list(), "user2", "wayne", True),
(create_user_list(), "user2", "user4", True),
(create_user_list(), "user2", "christoph", True),
(create_user_list(), "user2", "user19", True)])
def test_change_nickname(server, user_list,user_to_change, nickname, expected_result):
"""
Command: NICK
Parameters: <nickname> [ <hopcount> ]
NICK message is used to give user a nickname or change the previous
one. The <hopcount> parameter is only used by servers to indicate
how far away a nick is from its home server. A local connection has
a hopcount of 0. If supplied by a client, it must be ignored.
If a NICK message arrives at a server which already knows about an
identical nickname for another client, a nickname collision occurs.
As a result of a nickname collision, all instances of the nickname
are removed from the server's database, and a KILL command is issued
to remove the nickname from all other server's database. If the NICK
message causing the collision was a nickname change, then the
original (old) nick must be removed as well.
If the server recieves an identical NICK from a client which is
directly connected, it may issue an ERR_NICKCOLLISION to the local
client, drop the NICK command, and not generate any kills.
"""
server.user_list = create_user_list()
(create_user_dict(), "user2", "wayne", True),
(create_user_dict(), "user2", "user4", True),
(create_user_dict(), "user2", "christoph", True),
(create_user_dict(), "user2", "user19", True),
({"WiZ": {}}, "WiZ", "Kilroy", True)])
def test_change_nickname(server, user_dict, user_to_change, nickname, expected_result):
server.user_dict = user_dict.copy()
assert server.command_nickname(server, user_to_change, nickname) == expected_result
assert user_to_change not in server.user_list
if nickname not in user_list:
assert nickname in server.user_list
else:
assert nickname not in server.user_list
assert user_to_change not in server.user_dict
if nickname in user_dict:
assert nickname not in server.user_dict, nickname + " should have been deleted due to name collision"
Loading…
Cancel
Save