|
| 1 | +from os import path |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.core.files.uploadedfile import SimpleUploadedFile |
| 5 | +from django.template.defaultfilters import date |
| 6 | +from django.urls import reverse |
| 7 | +from model_mommy import mommy |
| 8 | + |
| 9 | +from pythonpro import settings |
| 10 | +from pythonpro.cohorts.models import Cohort |
| 11 | +from pythonpro.django_assertions import dj_assert_contains, dj_assert_not_contains |
| 12 | + |
| 13 | +_img_path = path.join(settings.BASE_DIR, 'pythonpro', 'core', 'static', 'img', 'instructors', 'renzo-nuccitelli.png') |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture |
| 17 | +def cohort(client, django_user_model): |
| 18 | + user = mommy.make(django_user_model) |
| 19 | + client.force_login(user) |
| 20 | + image = SimpleUploadedFile(name='renzo-nuccitelli.png', content=open(_img_path, 'rb').read(), |
| 21 | + content_type='image/png') |
| 22 | + cohort = mommy.make(Cohort, slug='guido-van-rossum', students=[user], image=image) |
| 23 | + return cohort |
| 24 | + |
| 25 | + |
| 26 | +@pytest.fixture |
| 27 | +def resp(client, cohort): |
| 28 | + resp = client.get(reverse('cohorts:detail', kwargs={'slug': cohort.slug}), secure=True) |
| 29 | + return resp |
| 30 | + |
| 31 | + |
| 32 | +def test_cohort_links_for_logged_user(client, django_user_model): |
| 33 | + user = mommy.make(django_user_model) |
| 34 | + client.force_login(user) |
| 35 | + image = SimpleUploadedFile(name='renzo-nuccitelli.png', content=open(_img_path, 'rb').read(), |
| 36 | + content_type='image/png') |
| 37 | + cohorts = mommy.make(Cohort, 4, image=image) |
| 38 | + resp = client.get('/', secure=True) |
| 39 | + for c in cohorts: |
| 40 | + dj_assert_contains(resp, c.get_absolute_url()) |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.django_db |
| 44 | +def test_cohort_links_not_avaliable_for_no_user(client): |
| 45 | + image = SimpleUploadedFile(name='renzo-nuccitelli.png', content=open(_img_path, 'rb').read(), |
| 46 | + content_type='image/png') |
| 47 | + cohorts = mommy.make(Cohort, 4, image=image) |
| 48 | + resp = client.get('/', secure=True) |
| 49 | + for c in cohorts: |
| 50 | + dj_assert_not_contains(resp, c.get_absolute_url()) |
| 51 | + |
| 52 | + |
| 53 | +def test_status_code(resp): |
| 54 | + assert 200 == resp.status_code |
| 55 | + |
| 56 | + |
| 57 | +@pytest.mark.parametrize('property_name', 'title mail_list forum_post'.split()) |
| 58 | +def test_cohort_propeties(cohort, resp, property_name): |
| 59 | + dj_assert_contains(resp, getattr(cohort, property_name)) |
| 60 | + |
| 61 | + |
| 62 | +def test_cohort_img(cohort: Cohort, resp): |
| 63 | + dj_assert_contains(resp, cohort.image.url) |
| 64 | + |
| 65 | + |
| 66 | +def test_cohort_start(cohort: Cohort, resp): |
| 67 | + dj_assert_contains(resp, date(cohort.start)) |
| 68 | + |
| 69 | + |
| 70 | +def test_cohort_end(cohort: Cohort, resp): |
| 71 | + dj_assert_contains(resp, date(cohort.end)) |
0 commit comments