-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
117 lines (86 loc) · 4.06 KB
/
tests.py
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
import os
import unittest
from unittest import TestCase
from config import SPOTIFY_CLIENT_ID, SPOTIFY_REDIRECT_URI
from model import connect_to_db, db, test_data
from views import app
import spotify
# http://flask.pocoo.org/docs/1.0/testing/
# https://requests-mock.readthedocs.io/en/latest/
class UserTest(unittest.TestCase):
"""Tests for user actions."""
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
connect_to_db(app, "postgresql:///testdb")
db.create_all()
test_data()
def test_register(self):
response = self.client.post('/register', data={'username': 'username', 'password': 'password'}, follow_redirects=True)
self.assertIn(b"Successfully created an account", response.data)
self.assertIn(b"See your top 40 artists on Spotify!", response.data)
def test_register_fail(self):
response = self.client.post('/register', data={'username': 'user1', 'password': 'password'}, follow_redirects=True)
self.assertIn(b"Sorry, an account with that username already exists", response.data)
def test_login(self):
response = self.client.post('/login', data={'username': 'user1', 'password': 'password'}, follow_redirects=True)
self.assertIn(b"See your top 40 artists on Spotify!", response.data)
def test_login_fail(self):
response = self.client.post('/login', data={'username': 'username', 'password': 'acdbe'}, follow_redirects=True)
self.assertIn(b"Sorry, that username or password", response.data)
def tearDown(self):
db.session.close()
db.drop_all()
class SpotifyTests(unittest.TestCase):
"""Tests Spotify login and authentication with user logged in."""
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
with self.client as c:
with c.session_transaction() as session:
session['user'] = 'user1'
def test_valid_auth_url(self):
auth_url = spotify.get_auth_url()
self.assertIn('user-top-read', auth_url)
self.assertIn(SPOTIFY_CLIENT_ID, auth_url)
def test_top40_spotify_logged_out(self):
response = self.client.get('/top-40', follow_redirects=True)
self.assertIn(b"Please login to Spotify", response.data)
def test_top40_spotify_logged_in(self):
response = self.client.get('/top-40', follow_redirects=True)
self.assertIn(b"Welcome", response.data)
class ViewTestsLoggedIn(unittest.TestCase):
"""View tests with user logged into session."""
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
with self.client as c:
with c.session_transaction() as session:
session['user'] = 'user1'
def test_login_spotify_page(self):
result = self.client.get('/get-top-40', follow_redirects=True)
self.assertIn(b"See your top 40 artists on Spotify!", result.data)
class ViewTestsLoggedOut(unittest.TestCase):
"""View tests without user logged into session."""
def setUp(self):
self.client = app.test_client()
app.config['TESTING'] = True
def test_homepage(self):
result = self.client.get('/')
self.assertIn(b"<h1>Login or register</h1>", result.data)
def test_register_page(self):
result = self.client.get('/register')
self.assertIn(b"Enter a username between 1-25 characters.", result.data)
def test_login_page(self):
result = self.client.get('/login')
self.assertIn(b"<h2>Login to your account</h2>", result.data)
def test_login_spotify_page_fail(self):
result = self.client.get('/get-top-40', follow_redirects=True)
# FIXME: Testing for flash message text doesn't work?
self.assertIn(b"Login or register", result.data)
def test_top40_fail(self):
result = self.client.get('/top-40', follow_redirects=True)
# FIXME: Testing for flash message text doesn't work?
self.assertIn(b"Login or register", result.data)
if __name__ == "__main__":
unittest.main()