summaryrefslogtreecommitdiffstats
path: root/askbot/tests/question_views_tests.py
blob: da3e081d1f5cb406202afc8496b81a99832f67b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from bs4 import BeautifulSoup
from askbot.conf import settings as askbot_settings
from askbot import const
from askbot.tests.utils import AskbotTestCase
from askbot import models
from django.core.urlresolvers import reverse


class PrivateQuestionViewsTests(AskbotTestCase):

    def setUp(self):
        self._backup = askbot_settings.GROUPS_ENABLED
        askbot_settings.update('GROUPS_ENABLED', True)
        self.user = self.create_user('user')
        self.group = models.Group.objects.create(
                        name='the group', openness=models.Group.OPEN
                    )
        self.user.join_group(self.group)
        self.qdata = {
            'title': 'test question title',
            'text': 'test question text'
        }
        self.client.login(user_id=self.user.id, method='force')

    def tearDown(self):
        askbot_settings.update('GROUPS_ENABLED', self._backup)

    def test_post_private_question(self):
        data = self.qdata
        data['post_privately'] = 'checked'
        response1 = self.client.post(reverse('ask'), data=data)
        response2 = self.client.get(response1['location'])
        dom = BeautifulSoup(response2.content)
        title = dom.find('h1').text
        self.assertTrue(unicode(const.POST_STATUS['private']) in title)
        question = models.Thread.objects.get()
        self.assertEqual(question.title, self.qdata['title'])
        self.assertFalse(models.Group.objects.get_global_group() in set(question.groups.all()))

        #private question is not accessible to unauthorized users
        self.client.logout()
        response = self.client.get(question._question_post().get_absolute_url())
        self.assertEqual(response.status_code, 302)
        self.assertEqual(response.content, '')
        #private question link is not shown on the main page
        #to unauthorized users
        response = self.client.get(reverse('questions'))
        self.assertFalse(self.qdata['title'] in response.content)
        #private question link is not shown on the poster profile
        #to the unauthorized users
        response = self.client.get(self.user.get_profile_url())
        self.assertFalse(self.qdata['title'] in response.content)

    def test_publish_private_question(self):
        question = self.post_question(user=self.user, is_private=True)
        title = question.thread.get_title()
        self.assertTrue(unicode(const.POST_STATUS['private']) in title)
        data = self.qdata
        #data['post_privately'] = 'false'
        data['select_revision'] = 'false'
        data['text'] = 'edited question text'
        response1 = self.client.post(
            reverse('edit_question', kwargs={'id':question.id}),
            data=data
        )
        response2 = self.client.get(question.get_absolute_url())
        dom = BeautifulSoup(response2.content)
        title = dom.find('h1').text
        self.assertTrue(models.Group.objects.get_global_group() in set(question.groups.all()))
        self.assertEqual(title, self.qdata['title'])

        self.client.logout()
        response = self.client.get(question.get_absolute_url())
        self.assertTrue('edited question text' in response.content)

    def test_privatize_public_question(self):
        question = self.post_question(user=self.user)
        title = question.thread.get_title()
        self.assertFalse(unicode(const.POST_STATUS['private']) in title)
        data = self.qdata
        data['post_privately'] = 'checked'
        data['select_revision'] = 'false'
        response1 = self.client.post(
            reverse('edit_question', kwargs={'id':question.id}),
            data=data
        )
        response2 = self.client.get(question.get_absolute_url())
        dom = BeautifulSoup(response2.content)
        title = dom.find('h1').text
        self.assertFalse(models.Group.objects.get_global_group() in set(question.groups.all()))
        self.assertTrue(unicode(const.POST_STATUS['private']) in title)

    def test_private_checkbox_is_on_when_editing_private_question(self):
        question = self.post_question(user=self.user, is_private=True)
        response = self.client.get(
            reverse('edit_question', kwargs={'id':question.id})
        )
        dom = BeautifulSoup(response.content)
        checkbox = dom.find(
            'input', attrs={'type': 'checkbox', 'name': 'post_privately'}
        )
        self.assertEqual(checkbox['checked'], 'checked')

    def test_private_checkbox_is_off_when_editing_public_question(self):
        question = self.post_question(user=self.user)
        response = self.client.get(
            reverse('edit_question', kwargs={'id':question.id})
        )
        dom = BeautifulSoup(response.content)
        checkbox = dom.find(
            'input', attrs={'type': 'checkbox', 'name': 'post_privately'}
        )
        self.assertEqual(checkbox.get('checked', False), False)


class PrivateAnswerViewsTests(AskbotTestCase):

    def setUp(self):
        self._backup = askbot_settings.GROUPS_ENABLED
        askbot_settings.update('GROUPS_ENABLED', True)
        self.user = self.create_user('user')
        group = models.Group.objects.create(
            name='the group', openness=models.Group.OPEN
        )
        self.user.join_group(group)
        self.question = self.post_question(user=self.user)
        self.client.login(user_id=self.user.id, method='force')

    def tearDown(self):
        askbot_settings.update('GROUPS_ENABLED', self._backup)

    def test_post_private_answer(self):
        response1 = self.client.post(
            reverse('answer', kwargs={'id': self.question.id}),
            data={'text': 'some answer text', 'post_privately': 'checked'}
        )
        answer = self.question.thread.get_answers(user=self.user)[0]
        self.assertFalse(models.Group.objects.get_global_group() in set(answer.groups.all()))
        self.client.logout()

        user2 = self.create_user('user2')
        self.client.login(user_id=user2.id, method='force')
        response = self.client.get(self.question.get_absolute_url())
        self.assertFalse('some answer text' in response.content)

        self.client.logout()
        response = self.client.get(self.question.get_absolute_url())
        self.assertFalse('some answer text' in response.content)

    def test_private_checkbox_is_on_when_editing_private_answer(self):
        answer = self.post_answer(
            question=self.question, user=self.user, is_private=True
        )
        response = self.client.get(
            reverse('edit_answer', kwargs={'id': answer.id})
        )
        dom = BeautifulSoup(response.content)
        checkbox = dom.find(
            'input', attrs={'type': 'checkbox', 'name': 'post_privately'}
        )
        self.assertEqual(checkbox['checked'], 'checked')

    def test_privaet_checkbox_is_off_when_editing_public_answer(self):
        answer = self.post_answer(question=self.question, user=self.user)
        response = self.client.get(
            reverse('edit_answer', kwargs={'id': answer.id})
        )
        dom = BeautifulSoup(response.content)
        checkbox = dom.find(
            'input', attrs={'type': 'checkbox', 'name': 'post_privately'}
        )
        self.assertEqual(checkbox.get('checked', False), False)

    def test_publish_private_answer(self):
        answer = self.post_answer(
            question=self.question, user=self.user, is_private=True
        )
        self.client.post(
            reverse('edit_answer', kwargs={'id': answer.id}),
            data={'text': 'edited answer text', 'select_revision': 'false'}
        )
        answer = self.reload_object(answer)
        self.assertTrue(models.Group.objects.get_global_group() in answer.groups.all())
        self.client.logout()
        response = self.client.get(self.question.get_absolute_url())
        self.assertTrue('edited answer text' in response.content)


    def test_privatize_public_answer(self):
        answer = self.post_answer(question=self.question, user=self.user)
        self.client.post(
            reverse('edit_answer', kwargs={'id': answer.id}),
            data={
                'text': 'edited answer text',
                'post_privately': 'checked',
                'select_revision': 'false'
            }
        )
        #check that answer is not visible to the "everyone" group
        answer = self.reload_object(answer)
        self.assertFalse(models.Group.objects.get_global_group() in answer.groups.all())
        #check that countent is not seen by an anonymous user
        self.client.logout()
        response = self.client.get(self.question.get_absolute_url())
        self.assertFalse('edited answer text' in response.content)