def test_follow_req(self): follower = User(username='******', password='******') followed = User(username='******', password='******') db.session.add(follower) db.session.add(followed) db.session.commit() self.login('follower', 'cat') url = url_for('main.follow') data = { 'followed_id': followed.id, } response = self.client.post(url, data=data) assert response.status_code == 200 self.assertTrue(follower.is_following(followed))
def test_auth(self): target = User(email='*****@*****.**', username='******', password='******') form = RegistrationForm(formdata=None, obj=target) data = { 'email': '*****@*****.**', 'username': '******', 'password': '******', 'password2': '123', } url = url_for('auth.register') response = self.client.post(url, data=data) assert response.status_code == 302 user = User.query.filter_by(username=target.username).first() assert user url = url_for('auth.login') response = self.login('admin', '123') assert response.status_code == 200 assert re.search('Hello,\s+admin!', response.get_data(as_text=True)) url = url_for('auth.logout') response = self.client.get(url, follow_redirects=True) assert response.status_code == 200 assert 'You have been logged out' in response.get_data(as_text=True)
def test_save(self): u = User( username = '******', password = '******', email = '*****@*****.**' ) us = UserService() us.find(5)
def register(user, password): user = User(user=user, password=password) db.session.add(user) try: db.session.commit() except IntegrityError as e: db.session.rollback() return False return True
def create_user(self, username: str, family_name: str, first_name: str, user_repository: IUserRepository): user_repository = user_repository user = User(UserId(str(uuid.uuid4())), FullName(family_name, first_name), UserName(username)) user_service = UserService(user_repository) if user_service.is_duplicated(user): raise ValueError("ユーザーネームが重複してます") user_repository.save(user)
def add_user(self, username: str, password: str): """ Add a user to the db :param username: :param password: """ session = self.Session() user = User(name=username.lower(), password=password) session.add(user) session.commit()
def init_admin(): fake = Faker() u = User(email=fake.email(), username='******', password='******', name=fake.name(), location=fake.city(), about_me=fake.text(), regist_time=fake.past_date()) db.session.add(u) db.session.commit()
async def login(self, login: str, password: str): # result bool or session result = False # search User u = await User.select_where( cls_fields=[User.id, User.name, User.email, User.description], conditions=[ User.name == login, User.password == User.p_encrypt(password) ]) # if isset if u: result = SessionManager().generate_session(data=dict(u[0])) return result
def register(): from entity import User from flasky import db form = RegistrationForm() if form.validate_on_submit(): user = User(email=form.email.data, username=form.username.data, password=form.password.data) db.session.add(user) db.session.commit() flash('You can now login.') return redirect(url_for('auth.login')) return render_template('auth/register.html', form=form)
def generate(self) -> None: """ Generate <count> users using faker library. :return: """ fake = Faker() for _ in range(self.count): self.users.append( User(fake.unique.email(), fake.password(), fake.first_name(), fake.last_name(), fake.date_of_birth(minimum_age=18, maximum_age=100), 'male' if fake.profile().get('sex') == 'M' else 'female', fake.city(), fake.sentence(nb_words=10)))
def _read_file(self, file_path: Path): """ :param file_path: :return: """ with open(file_path, 'r') as f: line = f.readline().rstrip().split('\t') # TODO: fix empty string while line and line != ['']: self.users.append(User(*line)) line = f.readline().rstrip().split('\t')
def users(count=10): init_admin() fake = Faker() i = 0 while i < count: u = User(email=fake.email(), username=fake.user_name(), password='******', name=fake.name(), location=fake.city(), about_me=fake.text(), regist_time=fake.past_date()) db.session.add(u) try: db.session.commit() i += 1 except IntegrityError: db.session.rollback()
#!/usr/bin/python # -*- coding: UTF-8 -*- from entity import User u = User() # 保存到数据库: sql = u.select() print sql
def save_user(self, username, password, email): user = User(username=username, password=password, email=email) self.__user_service.save(user)
def test_follow_logic(self): u1 = User(email='*****@*****.**', password='******') u2 = User(email='*****@*****.**', password='******') db.session.add(u1) db.session.add(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertFalse(u1.is_followed_by(u2)) timestamp_before = datetime.utcnow() u1.follow(u2) db.session.commit() timestamp_after = datetime.utcnow() self.assertTrue(u1.is_following(u2)) self.assertFalse(u1.is_followed_by(u2)) self.assertTrue(u2.is_followed_by(u1)) self.assertTrue(u1.followeds.count() == 1) self.assertTrue(u2.followers.count() == 1) f = u1.followeds.all()[-1] self.assertTrue(f.followed == u2) self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after) f = u2.followers.all()[-1] self.assertTrue(f.follower == u1) u1.unfollow(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertFalse(u1.is_followed_by(u2)) self.assertFalse(u2.is_following(u1)) self.assertFalse(u2.is_followed_by(u1)) self.assertTrue(u1.followeds.count() == 0) self.assertTrue(u2.followers.count() == 0) self.assertTrue(Follow.query.count() == 0) u2.follow(u1) db.session.commit() db.session.delete(u2) db.session.commit() self.assertTrue(Follow.query.count() == 0)
def test_password_salts_are_random(self): u = User(password='******') u2 = User(password='******') self.assertTrue(u.password_hash != u2.password_hash)
def test_password_verification(self): u = User(password='******') self.assertTrue(u.verify_password('cat')) self.assertFalse(u.verify_password('dog'))
def test_no_password_getter(self): u = User(password='******') with self.assertRaises(AttributeError): u.password
def test_password_setter(self): u = User(password='******') self.assertTrue(u.password_hash is not None)
def __init__(self): self.data = [] user_id = UserId(str(uuid.uuid4())) initial_user = User(user_id, FullName("piyo", "taro"), UserName("ppy")) self.data.append({user_id: initial_user})
def __init__(self): self.__db = DBSession.getInstance() User.create(self.__db.getEngine())
def update_user(self, username, password, email): user = User(username=username, password=password, email=email) self.__user_service.update(user)
def login(self, account, password): # 查询SQL语句 sql = " select id,account,password,name,permission,remark "+\ " from UserTbl "+\ " where account=%s and password=%s " with self.conn: cur = self.conn.cursor() values = [ account, password ] # 执行查询 cur.execute(sql, values) row = cur.fetchone() Id = row[0] name = row[3] permission = row[4] remark = row[5] # 封装用户信息 u = User() u.setId(Id) u.setAccount(account) u.setPassword(password) u.setName(name) u.setPermission(permission) u.setRemark(remark) return u return None