ASKBOT / askbot-devel

Askbot is a Django/Python Q&A forum. **Contributors README**: https://github.com/ASKBOT/askbot-devel#how-to-contribute. Commercial hosting of Askbot and support are available at https://askbot.com
Other
1.56k stars 628 forks source link

Cannot run test suite correctly #711

Open MichaelPereira opened 7 years ago

MichaelPereira commented 7 years ago

Problem description

Hi,

As I'm trying to make some changes into askbot, I wanted to make sure that I could run the test suite to make sure I'm not breaking anything in the process. However when trying to run the test suite, a significant amount of tests fail. The testing setup is explained after the tests results:

Ran 663 tests in 299.191s

FAILED (failures=1, errors=129, skipped=5)

All error cases except 3 fail with the same error as below:

======================================================================
ERROR: test_high_rep_user_can_downvote (askbot.tests.test_permission_assertions.VotePermissionAssertionTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/askbot/tests/test_permission_assertions.py", line 28, in setUp
    self.extraSetUp()
  File "/usr/local/lib/python2.7/site-packages/askbot/tests/test_permission_assertions.py", line 1425, in extraSetUp
    self.other_user = self.create_other_user()
  File "/usr/local/lib/python2.7/site-packages/askbot/tests/test_permission_assertions.py", line 36, in create_other_user
    email = 'other@test.com'
  File "/usr/local/lib/python2.7/site-packages/askbot/tests/utils.py", line 72, in create_user
    user = models.User.objects.create_user(username, email)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 187, in create_user
    **extra_fields)
  File "/usr/local/lib/python2.7/site-packages/django/contrib/auth/models.py", line 182, in _create_user
    user.save(using=self._db)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 734, in save
    force_update=force_update, update_fields=update_fields)
  File "/usr/local/lib/python2.7/site-packages/django/db/models/base.py", line 771, in save_base
    update_fields=update_fields, raw=raw, using=using)
  File "/usr/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 189, in send
    response = receiver(signal=self, sender=sender, **named)
  File "/usr/local/lib/python2.7/site-packages/askbot/models/__init__.py", line 3963, in add_user_to_default_groups
    instance.join_default_groups()
  File "/usr/local/lib/python2.7/site-packages/askbot/models/__init__.py", line 2778, in user_join_default_groups
    action='add'
  File "/usr/local/lib/python2.7/site-packages/askbot/models/__init__.py", line 3319, in user_edit_group_membership
    self.assert_can_join_or_leave_group()
  File "/usr/local/lib/python2.7/site-packages/askbot/models/__init__.py", line 853, in user_assert_can_join_or_leave_group
    suspended_user_cannot=True
  File "/usr/local/lib/python2.7/site-packages/askbot/models/__init__.py", line 735, in _assert_user_can
    raise django_exceptions.PermissionDenied(error_message)
PermissionDenied: Sorry, you cannot join or leave groups because your account is blocked.</br> Your account might be blocked in error - please contact the site administrators, if you think so.

I tried to look into how a user account gets blocked, but after looking the user model in both askbot and django, I still can't find what's behind user.is_blocked() to look into what is causing this problem exactly. Could you please give me a hint @evgenyfadeev ?

Reproducing

Since I was having issues running the tests locally on my mac laptop with tox, I created an optimized alpine-based docker image that runs the tests with the error above in a reproducible way. I'm planning to make a PR for it later after discussing it, but for now, you can check out the issue above by checking out that branch from my fork: https://github.com/MichaelPereira/askbot-devel/tree/docker-alpine-image then you can build the image and run the tests with:

./build-alpine.sh && ./test-alpine.sh
martin-bts commented 5 years ago

I had the same problem. It seems to be SQLite related. By default an in memory SQLite db is created specifically for the test. I replaced the SQLite with a regular Postgresql and all 129 errors magically disappeared.

What I did (I had to figure out what to do step by step so this is probably not the most efficient approach -- works on my machine):

Start a postgres instance

docker run -d \
    --name askbot-postgres-test \
    -p 5432:5432 \
    -e POSTGRES_PASSWORD=askbotPW \
    postgres:10

Setup a database as described on this page

echo localhost:5432:postgres:postgres:askbotPW >.pgpass
chmod 600 .pgpass
psql -h localhost -p 5432 -U postgres --no-password
#The following commands are sent to the psql cli
CREATE USER askbot WITH PASSWORD 'askB0T!';
ALTER ROLE askbot SET client_encoding TO 'utf8';
ALTER ROLE askbot SET default_transaction_isolation TO 'read committed';
ALTER ROLE asḱbot SET timezone TO 'UTC';
ALTER USER askbot CREATEDB;

Modify testproject/testproject/settings.py by replacing the line DATABASES = {'default': DATABASE} with

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'askbottest',
        'USER': 'askbot',
        'PASSWORD': 'askB0T!',
        'HOST': 'localhost',
        'PORT': '5432',
        'TEST_CHARSET': 'utf8',
    }
}

Have tox install psycopg2:

echo psycopg2-binary >> requirements-tests.txt

The reward for all of the above looks a lot like this:

----------------------------------------------------------------------                                                                                                                       
Ran 681 tests in 413.193s

OK (skipped=5)

Found 3 unused tags: picasso, pissarro, renoir.
Deleted.
Destroying test database for alias 'default'...
sebastian-philipp commented 5 years ago

So, the problem is that if any user is blocked, all next tests will fail when creating a new user, because the blocked status is re-used from the previous users.

sebastian-philipp commented 5 years ago

regarding postgres:

export DATABASE_URL='postgres://askbot:askB0T!@localhost/askbottest'

and this line needs to be removed:

-    'COLLATION': 'utf8_general_ci', # is necessary for MySQL tests to work properly.

create DB was missing

CREATE DATABASE askbottest OWNER=askbot;