tests.py 12.6 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
    def setUp(self):
        return super(FirstTimeLogIn, self).setUp()
70

Daniel W Bond's avatar
Daniel W Bond committed
71
72
    def tearDown(self):
        return super(FirstTimeLogIn, self).tearDown()
73

Daniel W Bond's avatar
Daniel W Bond committed
74
75
76
    def not_test_student_creation(self):
        # George Mason lands on Bookshare for the first time, and decides to
        # log in.
77

Daniel W Bond's avatar
Daniel W Bond committed
78
        # sign_in_user()
79

Daniel W Bond's avatar
Daniel W Bond committed
80
81
82
83
84
85
86
87
        # 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.

Daniel W Bond's avatar
Daniel W Bond committed
88
89
90
91
92
93
94
95
96
97
98
99
        # George wants to edit his name.

        # He clicks on the edit button.

        # He adds a 'IV' to the end of his last name.

        # George Mason IV then hits Save.

        # He's redirected to the user page...

        # ...where his name has been updated.

Daniel W Bond's avatar
Daniel W Bond committed
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
        # 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):
        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.
117
        self.browser.get(self.live_server_url)
118
119
        self.assertIn(u'SRCT Bookshare \u2022 Homepage', self.browser.title)

Daniel W Bond's avatar
Daniel W Bond committed
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
        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')
136

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

Daniel W Bond's avatar
Daniel W Bond committed
141
        # TODO: test javascript
142

Daniel W Bond's avatar
Daniel W Bond committed
143
144
        # He then fills out his post's remaining fields.
        course_abbr_input.send_keys('HIST 121')
145

Daniel W Bond's avatar
Daniel W Bond committed
146
147
        condition_input.select_by_value(u'Like New')
        access_code_input.select_by_value(u'Access Code NOT Included')
148

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

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

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

Daniel W Bond's avatar
Daniel W Bond committed
159
160
161
        # 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()
162

Daniel W Bond's avatar
Daniel W Bond committed
163
164
165
166
167
168
        # 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()
169

Daniel W Bond's avatar
Daniel W Bond committed
170
171
172
173
174
175
        # 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)
176

Daniel W Bond's avatar
Daniel W Bond committed
177
178
179
180
        # 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()
181

Daniel W Bond's avatar
Daniel W Bond committed
182
183
184
        # ...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)
185

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

Daniel W Bond's avatar
Daniel W Bond committed
189
190
191
192
193
194
        # ...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)
195

Daniel W Bond's avatar
Daniel W Bond committed
196
197
198
199
        # 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()
200

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

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

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

Daniel W Bond's avatar
Daniel W Bond committed
212
213
214
215
216
217
218
219
220
        # 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)
221

Daniel W Bond's avatar
Daniel W Bond committed
222
223
224
225
226
227
228
        # 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()
Daniel W Bond's avatar
Daniel W Bond committed
229
230
 
        # George goes to the Latest Listings page in the navbar.
231

Daniel W Bond's avatar
Daniel W Bond committed
232
        # He clicks on the listing for John Adams
233

Daniel W Bond's avatar
Daniel W Bond committed
234
        # George drives a hard bargain
235

Daniel W Bond's avatar
Daniel W Bond committed
236
        # sign_out_user()
237
238
239

        pass

Daniel W Bond's avatar
Daniel W Bond committed
240
    def not_test_flagging(self):
241

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

Daniel W Bond's avatar
Daniel W Bond committed
244
        # sign_in_user()
245

Daniel W Bond's avatar
Daniel W Bond committed
246
247
        # George clicks on the Search button in the navbar

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

        pass

Daniel W Bond's avatar
Daniel W Bond committed
252
    # note about SES!
Daniel W Bond's avatar
Daniel W Bond committed
253
    def not_test_exchanging(self):
254

Daniel W Bond's avatar
Daniel W Bond committed
255
256
257
258
259
260
261
        # self.browser.get('http://localhost:8000')

        # sign_in_user()

        # George goes to his user page.

        # He sees there is a bid on one of his listings.
262

Daniel W Bond's avatar
Daniel W Bond committed
263
        # He goes to his listing page and sees the listing.
Daniel W Bond's avatar
Daniel W Bond committed
264

Daniel W Bond's avatar
Daniel W Bond committed
265
266
267
268
269
270
271
272
273
        # Deeming it satisfactory, George decides to exchange his listing
        # with Abagail

        # George is fickle and gets cold feet.

        # He decides to leave his listing unsold, and await a better bid,
        # perhaps something in the text box.

        # sign_out_user()
274
275
276

        pass

Daniel W Bond's avatar
Daniel W Bond committed
277
278
    def not_test_rating(self):

Daniel W Bond's avatar
Daniel W Bond committed
279
280
281
        # self.browser.get('http://localhost:8000')

        # Some time has passed.
Daniel W Bond's avatar
Daniel W Bond committed
282

Daniel W Bond's avatar
Daniel W Bond committed
283
284
285
286
287
        # sign_in_user()

        # George has sold his listing to Abagail.

        # He decides to leave a rating.
Daniel W Bond's avatar
Daniel W Bond committed
288

Daniel W Bond's avatar
Daniel W Bond committed
289
290
291
292
293
294
        # He gives Abagail five out of five stars...

        # ...and leaves the following review.

        # Finished, he signs out.
        # sign_out_user()
Daniel W Bond's avatar
Daniel W Bond committed
295

296
297
        pass

Daniel W Bond's avatar
Daniel W Bond committed
298
299
300
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
301
302
303
304
    def setUp(self):
        # make sure that a Julius Caesar Lookout doesn't already exist
        return super(LookoutTests, self).setUp()

305
306
307
308
309
    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
310
        # George Mason wishes to create a lookout for a book for his class.
311
        self.browser.get(self.live_server_url)
312
        self.assertIn(u'SRCT Bookshare \u2022 Homepage', self.browser.title)
Daniel W Bond's avatar
Daniel W Bond committed
313

314
        sign_in_user(self)
Daniel W Bond's avatar
Daniel W Bond committed
315
316
317

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

320
321
322
323
324
        # 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
325
326
327

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

        # ...and then hits submit.
331
        self.browser.find_element_by_id('submit-id-submit').click()
Daniel W Bond's avatar
Daniel W Bond committed
332
333
334

        # 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.
335
336
        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
337
338
339

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

        # He's redirected to a confirmation page. George clicks 'delete'.
343
344
345
        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
346
347

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

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