test_views.py 3.29 KB
Newer Older
muzaffaryousaf committed
1 2 3 4 5 6
# -*- coding: utf-8 -*-
""" Tests for student profile views. """

from django.conf import settings
from django.core.urlresolvers import reverse
from django.test import TestCase
7
from django.test.client import RequestFactory
muzaffaryousaf committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

from util.testing import UrlResetMixin
from student.tests.factories import UserFactory

from student_profile.views import learner_profile_context


class LearnerProfileViewTest(UrlResetMixin, TestCase):
    """ Tests for the student profile view. """

    USERNAME = "username"
    PASSWORD = "password"
    CONTEXT_DATA = [
        'default_public_account_fields',
        'accounts_api_url',
        'preferences_api_url',
        'account_settings_page_url',
        'has_preferences_access',
        'own_profile',
        'country_options',
        'language_options',
29 30
        'account_settings_data',
        'preferences_data',
muzaffaryousaf committed
31 32 33 34 35 36 37 38 39 40 41
    ]

    def setUp(self):
        super(LearnerProfileViewTest, self).setUp()
        self.user = UserFactory.create(username=self.USERNAME, password=self.PASSWORD)
        self.client.login(username=self.USERNAME, password=self.PASSWORD)

    def test_context(self):
        """
        Verify learner profile page context data.
        """
42
        request = RequestFactory().get('/url')
43
        request.user = self.user
44

45
        context = learner_profile_context(request, self.USERNAME, self.user.is_staff)
muzaffaryousaf committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

        self.assertEqual(
            context['data']['default_public_account_fields'],
            settings.ACCOUNT_VISIBILITY_CONFIGURATION['public_fields']
        )

        self.assertEqual(
            context['data']['accounts_api_url'],
            reverse("accounts_api", kwargs={'username': self.user.username})
        )

        self.assertEqual(
            context['data']['preferences_api_url'],
            reverse('preferences_api', kwargs={'username': self.user.username})
        )

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
        self.assertEqual(
            context['data']['profile_image_upload_url'],
            reverse("profile_image_upload", kwargs={'username': self.user.username})
        )

        self.assertEqual(
            context['data']['profile_image_remove_url'],
            reverse('profile_image_remove', kwargs={'username': self.user.username})
        )

        self.assertEqual(
            context['data']['profile_image_max_bytes'],
            settings.PROFILE_IMAGE_MAX_BYTES
        )

        self.assertEqual(
            context['data']['profile_image_min_bytes'],
            settings.PROFILE_IMAGE_MIN_BYTES
        )

muzaffaryousaf committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95
        self.assertEqual(context['data']['account_settings_page_url'], reverse('account_settings'))

        for attribute in self.CONTEXT_DATA:
            self.assertIn(attribute, context['data'])

    def test_view(self):
        """
        Verify learner profile page view.
        """
        profile_path = reverse('learner_profile', kwargs={'username': self.USERNAME})
        response = self.client.get(path=profile_path)

        for attribute in self.CONTEXT_DATA:
            self.assertIn(attribute, response.content)
96 97 98 99 100 101 102 103

    def test_undefined_profile_page(self):
        """
        Verify that a 404 is returned for a non-existent profile page.
        """
        profile_path = reverse('learner_profile', kwargs={'username': "no_such_user"})
        response = self.client.get(path=profile_path)
        self.assertEqual(404, response.status_code)