def create_new_user(self, email: str, password: str): email = email.lower() self.validate_user_email_password(email, password) user = User(email=email, password=password) user.hash_password(password) # set is_confirm to true user.is_confirmed = True return self.user_repo.create(user)
def get_by_name(self, name: str, credentials: Credentials) -> User: logger.info(f'GET {app_config.USER_SERVICE_URL}') # TODO: aiohttp r = requests.get(app_config.USER_SERVICE_URL, auth=(credentials.username, credentials.password)) if r.status_code != 200: raise UserServiceConnectionError(f'Status code {r.status_code} for GET {app_config.USER_SERVICE_URL}') user: User = User(**r.json()) return user
def test_create_token(self): connector = MagicMock() connector.get_by_name = MagicMock(return_value=User(nickname='Robin')) self.service = TokenService(connector) token = self.service.create_access_token('Robin', Credentials('a', 'b')) user = self.service.get_by_token(token) self.assertEqual('Robin', user.nickname)
def sign_up_new_user(self, email: str, password: str, confirm_url: str): self.validate_user_email_password(email, password) user: User = User(email=email, password=password) user.hash_password(password) # set is_confirm to False user.is_confirmed = False u = self.user_repo.create(user) if self.email_service: self.email_service.send_confirm_email(user.email, confirm_url, template='user/activate.html') return u
def update_scope(name: str, scopes: List[str]) -> User: logger.info(f'Updating scope of user {name} with {scopes}') with transaction_context() as session: user = session.query(DbUser).filter_by(nickname=name).first() if user is None: raise Exception(f'No user named {name}') user.scopes = ' '.join(scopes) session.commit() ret = User(**user.serialize) return ret
def create_user(name: str, pwd: str, scopes: List[str] = None) -> User: logger.info(f'Creating user {name}') user = DbUser(nickname=name, password=pwd, scopes=' '.join(scopes) if scopes else '') with transaction_context() as session: session.add(user) session.commit() ret = User(**user.serialize) return ret
def RegisterUser(self, pEmail): uid = "1234" user, error = self.service.Duplicated(pEmail) if error != None: return None, error newUser = User(uid, pEmail) savedUser, error = self.repository.Save(newUser) if error != None: return error return savedUser, None
def test_cache_refresh(self): connector = MagicMock() connector.get_by_name = MagicMock(return_value=User(nickname='Robin')) self.service = TokenService(connector) token = self.service.create_access_token('Robin', Credentials('a', 'b'), timedelta(seconds=2)) user = self.service.get_by_token(token) self.assertEqual('Robin', user.nickname) time.sleep(1) self.service._refresh_cache() user = self.service.get_by_token(token) self.assertEqual('Robin', user.nickname) time.sleep(2) self.service._refresh_cache() self.assertRaises(TokenNotFound, self.service.get_by_token, token)
def setUp(self): connector = MagicMock() connector.get_by_name = MagicMock( return_value=User(nickname='Robin', scopes_allowed=['me'])) self.token_service = TokenService(connector) self.client = TestClient(api)
def test_user_has_scope_succeed(self): user = User(nickname='R', scopes_allowed=['a', 'b']) self.assertFalse(user.has_scopes(['a', 'c']))
def test_user_has_scope_fail(self): user = User(nickname='R', scopes_allowed=['a', 'b']) self.assertTrue(user.has_scopes(['a']))
def test_service(self): connector = MagicMock() connector.get_by_name = MagicMock(return_value=User(nickname='Robin')) self.service = TokenService(connector) user = self.service.get_by_token('aaa', Credentials('a', 'b')) print(user)
def get_all_users() -> List[User]: with transaction_context() as session: users_from_db: List[DbUser] = session.query(DbUser).all() users = list(map(lambda x: User(**x.serialize), users_from_db)) return users
def create_user(user_id: str, name: str, email: str) -> User: return User(user_id=UserId(user_id), name=Name(name), email=Email(email))