def test_authenticate(collection): User.create('me', 'password') u = User.get('me', 'password') auth_key = u.authenticate() u = User.get('me', 'password') assert u.dbrow['auth_keys'] == auth_key, 'Authkey not saved correctly'
def test_authenticate_twice(collection): User.create('me', 'password') u = User.get('me', 'password') auth_key = u.authenticate() u = User.get('me', 'password') auth_key2 = u.authenticate() u = User.get('me', 'password') got1, got2 = u.dbrow['auth_keys'].split(' '.encode('utf-8')) assert got1 == auth_key assert got2 == auth_key2
def on_message(self, message): message_dict = json.loads(message) message_id = message_dict.get('id', 0) if 'subscribe' in message_dict: user = message_dict.get('username') auth_key = message_dict.get('authkey') if User.is_authenticated(user, auth_key): self.subscriptions.extend(message_dict.get('subscribe')) if 'register' in message_dict: name = message_dict['register'].get('name') password = message_dict['register'].get('password') try: user_id = User.create(name, password) except UserExists: self.send_exception(message_id, 'User with that name already exists') except Exception as e: print(e) self.send_exception(message_id, 'Unknown problem') else: self.write_message(json.dumps({ 'id': message_id, 'type': 'registration', 'user_id': str(user_id), })) if 'authenticate' in message_dict: name = message_dict['authenticate'].get('name') password = message_dict['authenticate'].get('password') try: u = User.get(name, password) except Exception: self.send_exception(message_id, 'Invalid username/password') else: auth_key = u.authenticate() self.write_message(json.dumps({ 'id': message_id, 'type': 'authenticate', 'key': auth_key, }))
def test_user_get_failed_password(mock_get, collection): User.create('me', 'password') with pytest.raises(Exception): User.get('me', 'passwd2')
def test_is_authenticated_fail(collection): User.create('me', 'password') User.get('me', 'password') assert not User.is_authenticated('me', 'aa'.encode('utf-8')), 'User isn\'t authenticated'
def test_is_authenticated(collection): User.create('me', 'password') u = User.get('me', 'password') auth_key = u.authenticate() assert User.is_authenticated('me', auth_key), 'User isn\'t authenticated'