I am trying to test a view which uses info from the current_user to create a model, store an instance of it and return a page with the model properties. Unfortunately, I have problem in this view while working with the current_user. It seems that I cant simulate in the view the logging of an user because the view returns an AnonymousUser error (AttributeError: 'AnonymousUser' object has no attribute '_sa_instance_state' ).
How can I "fake" a logged user in the test so the test of the view passes?
Here a copy of the test and view
Thanks!
@api_blueprint.route('myurl', methods=['POST'])
def view():
d = does_something(request.form)
qo = models.Model(
user=current_user, # HERE IS THE PROBLEM WITH THE LOGGING SINCE IN THE TEST IT IS AN AnonymousUser
dump=d)
)
manager.session.add(qo)
manager.session.commit()
return render_template(
'my_page.html',
)
def test_view(self):
query = {
'foo': ['1', '2'],
}
self.login(test@example.com, password) # It seems it does not log an user but the test for this passed
response = self.app.post(
'myurl',
data=query,
)
def login(self, username, password):
return self.app.post('/login', data=dict(
username=username,
password=password
), follow_redirects=True)