One of the aims of my latest Python Flask project is to make sure all site functionality is accompanied by a set of unit tests, partly to figure out how to use a test framework and also to maintain discipline with the project development.
The recent inclusion of LDAP authentication for the site poses a problem, however, for a set of unit tests that add, edit and delete content in the database. There is also the complication that the application includes activity logging for those actions and adds a username against each record: just setting ‘LOGIN_DISABLED = True’ isn’t going to work; we need an alternative user model available during the unit tests.
A simple model can just be of the form of a class added to the test script
class UnittestUser(): def __init__(self): self.username = "unittest" def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return(1) def get_name(self): return(self.username)
crossword_hints.login_manager.anonymous_user = UnittestUser
All the insert, update and delete tests complete and the temporary (sqlite3) databases record the username in the activity_logs table.
Apologies for the lack of references in this post, but some of this is pushing up against the limits of my Python knowledge and there was no one source that gave the answers. There was a post on SO regading the anonymous user class but it is not showing up in my browser history.
The flask-login site provided some details but the information is quite vague and you can never be sure whether it is going to solve the problem or not.