tests.py 11.8 KB
Newer Older
Daniel W Bond's avatar
Daniel W Bond committed
1
2
# functional tests describe a 'user story', testing how the implementation works
# with a complete black box as to how it works on the backend
3

4
from django.contrib.staticfiles.testing import StaticLiveServerTestCase
5
from selenium import webdriver
Daniel W Bond's avatar
Daniel W Bond committed
6
7
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
8

9
10
11
12
from django.contrib.auth.models import User
from core.models import Student
from trades.models import Listing

Daniel W Bond's avatar
Daniel W Bond committed
13

14
15
16
# your mason username and password are neccessary to log in for test cases
username = 'gmason'
password = 'Gunston_Ha11'
Daniel W Bond's avatar
Daniel W Bond committed
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

def sign_in_user(self):
    """ Hits the proper buttons to log a student in through Mason CAS."""

    # He lands on the the front page, and decide to log in.
    self.browser.find_element_by_link_text('Log In').click()

    # He is redirected login.gmu.edu, where he sees the username and
    # password fields.
    self.assertIn('Mason Central Authentication Service', self.browser.title)
    username_input = self.browser.find_element_by_id('username')
    password_input = self.browser.find_element_by_id('password')

    # He types in his username and password...
    username_input.send_keys(username)
    password_input.send_keys(password)

    # ...and hits the submit button.
    self.browser.find_element_by_class_name('btn-submit').click()

    # George is then redirected back to the homepage.
    self.assertIn(u'SRCT Bookshare \u2022 Homepage', self.browser.title)

def sign_out_user(self):
    """ Hits the proper buttons to log a student out through Mason CAS."""

    # George is on a nonadmin page with SRCT Bookshare, and wants to log out.
    self.browser.find_element_by_link_text('Log Out').click()

    # He is redirected to login.gmu.edu...
    self.assertIn('Mason Central Authentication Service', self.browser.title)

    # and sees a successful logout message.
    self.assertIn(u'Logout successful', self.browser.find_element_by_tag_name('h2').text)
51

52
class SeleniumSetUpTearDown(StaticLiveServerTestCase):
Daniel W Bond's avatar
Daniel W Bond committed
53
    """TestCase subclass to add Selenium setup and teardown."""
54
55
56
57
58
59

    def setUp(self):
        self.browser = webdriver.Firefox()
        self.browser.implicitly_wait(3)

    def tearDown(self):
60
61
        pass
        # self.browser.quit()
62
63


Daniel W Bond's avatar
Daniel W Bond committed
64
65
66
class FirstTimeLogIn(SeleniumSetUpTearDown):
    """Tests that a Student is created and all their attributes set on a user's
       initial login."""
67

Daniel W Bond's avatar
Daniel W Bond committed
68
69
70
71
72
73
74
    def setUp(self):
        # in the database
        # other Student
        # good Listing (other Student)
        # bad Listing (other Student)
        # George's other Listing
        return super(FirstTimeLogIn, self).setUp()
75

Daniel W Bond's avatar
Daniel W Bond committed
76
77
    def tearDown(self):
        return super(FirstTimeLogIn, self).tearDown()
78

Daniel W Bond's avatar
Daniel W Bond committed
79
80
81
    def not_test_student_creation(self):
        # George Mason lands on Bookshare for the first time, and decides to
        # log in.
82

Daniel W Bond's avatar
Daniel W Bond committed
83
        # sign_in_user()
84

Daniel W Bond's avatar
Daniel W Bond committed
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
        # He lands on the homepage (not an error page!)

        # He then clicks on his gravatar icon in the navbar...

        # ...and is redirected to his user page.

        # His full name and email are displayed on his user page.

        # George has to run, so he hits the log out button, but he's excited
        # to return later.        
        # sign_out_user()
        pass

class ListingTests(SeleniumSetUpTearDown):
    """Tests all the user interactions pertaining to the models in the trades app."""

    def setUp(self):
        # other Student
        # good Listing (other Student)
        # bad Listing (other Student)
        # George's other Listing
        return super(ListingTests, self).setUp()

    def tearDown(self):
        return super(ListingTests, self).tearDown()

    def test_listing_management(self):
        # George Mason has previously used Bookshare, but wants to sign in and
        # create a new Listing.
114
        self.browser.get(self.live_server_url)
115
116
        self.assertIn(u'SRCT Bookshare \u2022 Homepage', self.browser.title)

Daniel W Bond's avatar
Daniel W Bond committed
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
        sign_in_user(self)

        # He clicks on 'Create Listing' button in the navbar to add his new
        # textbook.
        self.browser.find_element_by_link_text('Create Listing').click()

        # He his sent to the Create Listing page, where he sees a number of
        # fields to post his Listing.
        self.assertIn(u'SRCT Bookshare \u2022 Create Listing', self.browser.title)
        isbn_input = self.browser.find_element_by_id('id_isbn')
        course_abbr_input = self.browser.find_element_by_id('id_course_abbr')
        condition_input = Select(self.browser.find_element_by_id('id_condition'))
        access_code_input = Select(self.browser.find_element_by_id('id_access_code'))
        price_input = self.browser.find_element_by_id('id_price')
        # TODO: uploading a photo
        description_input = self.browser.find_element_by_id('id_description')
133

Daniel W Bond's avatar
Daniel W Bond committed
134
135
136
        # He types in his ISBN...
        isbn_input.send_keys('0807830534')
        # ...and is pleasantly surprised to see the title and more autocompleted.
137

Daniel W Bond's avatar
Daniel W Bond committed
138
        # TODO: test javascript
139

Daniel W Bond's avatar
Daniel W Bond committed
140
141
        # He then fills out his post's remaining fields.
        course_abbr_input.send_keys('HIST 121')
142

Daniel W Bond's avatar
Daniel W Bond committed
143
144
        condition_input.select_by_value(u'Like New')
        access_code_input.select_by_value(u'Access Code NOT Included')
145

Daniel W Bond's avatar
Daniel W Bond committed
146
147
        price_input.send_keys('20')
        description_input.send_keys('I would be willing to trade this book for one that I need next semester.')
148

Daniel W Bond's avatar
Daniel W Bond committed
149
150
        # George has gotten to the bottom of the page and clicks on Submit...
        self.browser.find_element_by_id('submit-id-submit').click()
151

Daniel W Bond's avatar
Daniel W Bond committed
152
153
154
        # ...where he is redirected to his new listing's page.
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder',
                      self.browser.title)
155

Daniel W Bond's avatar
Daniel W Bond committed
156
157
158
        # George thinks he may have acted in haste, so decides to cancel his
        # Listing. He sees the 'Cancel Listing' button and clicks it.
        self.browser.find_element_by_link_text('Cancel Listing').click()
159

Daniel W Bond's avatar
Daniel W Bond committed
160
161
162
163
164
165
        # George then sees the confirmation page...
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder \u2022 Cancel',
                      self.browser.title)
        
        # and elects follow through with the cancellation.
        self.browser.find_element_by_xpath("//input[@value='Cancel Your Listing']").click()
166

Daniel W Bond's avatar
Daniel W Bond committed
167
168
169
170
171
172
        # He is then redirected back to his listing page, and now there's a
        # large danger alert saying the listing has been cancelled.
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder',
                      self.browser.title)
        self.assertIn(u'This listing has been cancelled.',
                      self.browser.find_element_by_class_name('alert-danger').text)
173

Daniel W Bond's avatar
Daniel W Bond committed
174
175
176
177
        # George however is indecisive, and decides he in fact does want the
        # the listing open for others to bid on it. He clicks on the 'Reopen
        # Listing' button...
        self.browser.find_element_by_link_text('Reopen Listing').click()
178

Daniel W Bond's avatar
Daniel W Bond committed
179
180
181
        # ...and is sent to a confirmation page to reopen his Listing.
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder \u2022 Reopen',
                      self.browser.title)
182

Daniel W Bond's avatar
Daniel W Bond committed
183
184
        # He clicks to confirm he wants his listing back open...
        self.browser.find_element_by_xpath("//input[@value='Reopen Your Listing']").click()
185

Daniel W Bond's avatar
Daniel W Bond committed
186
187
188
189
190
191
        # ...and is sent back to the Listing's page, this time sans any alerts
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder',
                      self.browser.title)
        with self.assertRaises(NoSuchElementException):
            self.assertIn(u'This listing has been cancelled.',
                          self.browser.find_element_by_class_name('alert-danger').text)
192

Daniel W Bond's avatar
Daniel W Bond committed
193
194
195
196
        # George thinks he's offering his textbook for a little too low of a
        # price, so decides to edit his Listing to increase the price. He clicks
        # on the 'Edit Listing' button...
        self.browser.find_element_by_link_text('Edit Listing').click()
197

Daniel W Bond's avatar
Daniel W Bond committed
198
199
200
        # ...where he's sent to a Listing editing page.
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder \u2022 Edit',
                      self.browser.title)
201

Daniel W Bond's avatar
Daniel W Bond committed
202
203
        # He sees the fields on the page and finds the one for price.
        price_input = self.browser.find_element_by_id('id_price')
204

Daniel W Bond's avatar
Daniel W Bond committed
205
206
207
        # George increases his asking price for the textbook.
        price_input.clear()
        price_input.send_keys('30')
208

Daniel W Bond's avatar
Daniel W Bond committed
209
210
211
212
213
214
215
216
217
        # He then hits Submit...
        self.browser.find_element_by_xpath("//input[@value='Update']").click()
        
        # and is sent back to the Listing page...
        self.assertIn(u'SRCT Bookshare \u2022 George Mason : Forgotten Founder',
                      self.browser.title)
       
        # and his asking price has also been updated.
        self.assertIn(u'$30', self.browser.find_element_by_class_name('price').text)
218

Daniel W Bond's avatar
Daniel W Bond committed
219
220
221
222
223
224
225
        # George is finished up for now, so he hits the log out button on the navbar.
        sign_out_user(self)

    def not_test_bidding(self):
        # self.browser.get('http://localhost:8000')

        # sign_in_user()
226

Daniel W Bond's avatar
Daniel W Bond committed
227
        # George finds himself on someone else's listing page.
228

Daniel W Bond's avatar
Daniel W Bond committed
229
        # George drives a hard bargain
230

Daniel W Bond's avatar
Daniel W Bond committed
231
        # sign_out_user()
232
233
234

        pass

Daniel W Bond's avatar
Daniel W Bond committed
235
    def not_test_flagging(self):
236

Daniel W Bond's avatar
Daniel W Bond committed
237
        # self.browser.get('http://localhost:8000')
238

Daniel W Bond's avatar
Daniel W Bond committed
239
        # sign_in_user()
240

Daniel W Bond's avatar
Daniel W Bond committed
241
        # sign_out_user()
242
243
244

        pass

Daniel W Bond's avatar
Daniel W Bond committed
245
    def not_test_exchanging(self):
246

Daniel W Bond's avatar
Daniel W Bond committed
247
       # self.browser.get('http://localhost:8000')
248

Daniel W Bond's avatar
Daniel W Bond committed
249
250
251
       # sign_in_user()

       # sign_out_user()
252
253
254

        pass

Daniel W Bond's avatar
Daniel W Bond committed
255
256
257
258
259
260
261
262
    def not_test_rating(self):

       # self.browser.get('http://localhost:8000')

       # sign_in_user()

       # sign_out_user()

263
264
        pass

Daniel W Bond's avatar
Daniel W Bond committed
265
266
267
class LookoutTests(SeleniumSetUpTearDown):
    """Tests all the user interactions pertaining to the models in the lookouts app."""

Daniel W Bond's avatar
Daniel W Bond committed
268
269
270
271
    def setUp(self):
        # make sure that a Julius Caesar Lookout doesn't already exist
        return super(LookoutTests, self).setUp()

272
273
274
275
276
    def tearDown(self):
        # delete the George Mason Lookout
        return super(LookoutTests, self).tearDown()

    def test_lookout_management(self):
Daniel W Bond's avatar
Daniel W Bond committed
277
        # George Mason wishes to create a lookout for a book for his class.
278
        self.browser.get(self.live_server_url)
279
        self.assertIn(u'SRCT Bookshare \u2022 Homepage', self.browser.title)
Daniel W Bond's avatar
Daniel W Bond committed
280

281
        sign_in_user(self)
Daniel W Bond's avatar
Daniel W Bond committed
282
283
284

        # George decides to create a lookout by clicking the Create button
        # on the front page.
285
        self.browser.find_element_by_link_text('Create').click()
Daniel W Bond's avatar
Daniel W Bond committed
286

287
288
289
290
291
        # He is sent to the Lookout creation page...
        self.assertIn(u'SRCT Bookshare \u2022 Create Lookout', self.browser.title)

        # and then sees the ISBN field.
        isbn_input = self.browser.find_element_by_id('id_isbn')
Daniel W Bond's avatar
Daniel W Bond committed
292
293
294

        # He types in the ISBN of a textbook he'd like to automatically
        # search for...
295
        isbn_input.send_keys('0743482743')
Daniel W Bond's avatar
Daniel W Bond committed
296
297

        # ...and then hits submit.
298
        self.browser.find_element_by_id('submit-id-submit').click()
Daniel W Bond's avatar
Daniel W Bond committed
299
300
301

        # He is then redirected to the lookout detail page, where he can see a
        # lookout has been created, and all the listings to choose from.
302
303
        self.assertIn(u'SRCT Bookshare \u2022 Lookouts \u2022 The Tragedy Of Julius Caesar',
                      self.browser.title)
Daniel W Bond's avatar
Daniel W Bond committed
304
305
306

        # George however decides that he doesn't actually need a lookout for
        # this ISBN, and clicks the delete button.
307
        self.browser.find_element_by_link_text('Delete this Lookout').click()
Daniel W Bond's avatar
Daniel W Bond committed
308
309

        # He's redirected to a confirmation page. George clicks 'delete'.
310
311
312
        self.assertIn(u'SRCT Bookshare \u2022 Delete Lookout',
                      self.browser.title)
        self.browser.find_element_by_xpath("//input[@value='Confirm']").click()
Daniel W Bond's avatar
Daniel W Bond committed
313
314

        # George gets redirected back the the homepage.
315
        self.assertIn(u'SRCT Bookshare \u2022 Homepage', self.browser.title)
Daniel W Bond's avatar
Daniel W Bond committed
316
317

        # Finished, he hits the log out button in the navbar.
318
        sign_out_user(self)