Question
Python unit test Can anyone help me to add 2 unit test in the test web file for the any classes in the user file
Python unit test
Can anyone help me to add 2 unit test in the test web file for the any classes in the user file or form file? i appreicate it.
User file
""" User classes & helpers ~~~~~~~~~~~~~~~~~~~~~~ """ import os import json import binascii import hashlib from functools import wraps
from flask import current_app from flask_login import current_user
class UserManager(object): """A very simple user Manager, that saves it's data as json.""" def __init__(self, path): self.file = os.path.join(path, 'users.json')
def read(self): if not os.path.exists(self.file): return {} with open(self.file) as f: data = json.loads(f.read()) return data
def write(self, data): with open(self.file, 'w') as f: f.write(json.dumps(data, indent=2))
def add_user(self, name, password, active=True, roles=[], authentication_method=None): users = self.read() if users.get(name): return False if authentication_method is None: authentication_method = get_default_authentication_method() new_user = { 'active': active, 'roles': roles, 'authentication_method': authentication_method, 'authenticated': False } # Currently we have only two authentication_methods: cleartext and # hash. If we get more authentication_methods, we will need to go to a # strategy object pattern that operates on User.data. if authentication_method == 'hash': new_user['hash'] = make_salted_hash(password) elif authentication_method == 'cleartext': new_user['password'] = password else: raise NotImplementedError(authentication_method) users[name] = new_user self.write(users) userdata = users.get(name) return User(self, name, userdata)
def get_user(self, name): users = self.read() userdata = users.get(name) if not userdata: return None return User(self, name, userdata)
def delete_user(self, name): users = self.read() if not users.pop(name, False): return False self.write(users) return True
def update(self, name, userdata): data = self.read() data[name] = userdata self.write(data)
class User(object): def __init__(self, manager, name, data): self.manager = manager self.name = name self.data = data
def get(self, option): return self.data.get(option)
def set(self, option, value): self.data[option] = value self.save()
def save(self): self.manager.update(self.name, self.data)
def is_authenticated(self): return self.data.get('authenticated')
def is_active(self): return self.data.get('active')
def is_anonymous(self): return False
def get_id(self): return self.name
def check_password(self, password): """Return True, return False, or raise NotImplementedError if the authentication_method is missing or unknown.""" authentication_method = self.data.get('authentication_method', None) if authentication_method is None: authentication_method = get_default_authentication_method() # See comment in UserManager.add_user about authentication_method. if authentication_method == 'hash': result = check_hashed_password(password, self.get('hash')) elif authentication_method == 'cleartext': result = (self.get('password') == password) else: raise NotImplementedError(authentication_method) return result
def get_default_authentication_method(): return current_app.config.get('DEFAULT_AUTHENTICATION_METHOD', 'cleartext')
def make_salted_hash(password, salt=None): if not salt: salt = os.urandom(64) d = hashlib.sha512() d.update(salt[:32]) d.update(password) d.update(salt[32:]) return binascii.hexlify(salt) + d.hexdigest()
def check_hashed_password(password, salted_hash): salt = binascii.unhexlify(salted_hash[:128]) return make_salted_hash(password, salt) == salted_hash
def protect(f): @wraps(f) def wrapper(*args, **kwargs): if current_app.config.get('PRIVATE') and not current_user.is_authenticated: return current_app.login_manager.unauthorized() return f(*args, **kwargs) return wrapper
Form file
""" Forms ~~~~~ """ from flask_wtf import Form from wtforms import BooleanField from wtforms import TextField from wtforms import TextAreaField from wtforms import PasswordField from wtforms.validators import InputRequired from wtforms.validators import ValidationError
from wiki.core import clean_url from wiki.web import current_wiki from wiki.web import current_users
class URLForm(Form): url = TextField('', [InputRequired()])
def validate_url(form, field): if current_wiki.exists(field.data): raise ValidationError('The URL "%s" exists already.' % field.data)
def clean_url(self, url): return clean_url(url)
class SearchForm(Form): term = TextField('', [InputRequired()]) ignore_case = BooleanField( description='Ignore Case', # FIXME: default is not correctly populated default=True)
class EditorForm(Form): title = TextField('', [InputRequired()]) body = TextAreaField('', [InputRequired()]) tags = TextField('')
class LoginForm(Form): name = TextField('', [InputRequired()]) password = PasswordField('', [InputRequired()])
def validate_name(form, field): user = current_users.get_user(field.data) if not user: raise ValidationError('This username does not exist.')
def validate_password(form, field): user = current_users.get_user(form.name.data) if not user: return if not user.check_password(field.data): raise ValidationError('Username and password do not match.')
Test file
from . import WikiBaseTestCase
class WebContentTestCase(WikiBaseTestCase): """ Various test cases around web content. """
def test_index_missing(self): """ Assert the wiki will correctly play the content missing index page, if no index page exists. """ rsp = self.app.get('/') assert b"You did not create any content yet." in rsp.data assert rsp.status_code == 200
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started