-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.py
More file actions
140 lines (116 loc) · 4.06 KB
/
Copy pathtests.py
File metadata and controls
140 lines (116 loc) · 4.06 KB
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
import os
import unittest
import tempfile
from app import app, db
from config import basedir
from app.models import User, Book, Author
class appTestCase(unittest.TestCase):
def setUp(self):
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'test.db')
self.app = app.test_client()
db.create_all()
self.create_user()
def tearDown(self):
db.session.remove()
db.drop_all()
def create_user(self):
user = User(login='admin', password='admin', email='admin@admin.com')
db.session.add(user)
db.session.commit()
def create_book(self):
author = Author(name='auth name')
book = Book(title='New Book', authors=[author], description='some desc')
db.session.add(book)
db.session.commit()
def create_author(self):
author = Author(name='auth name')
db.session.add(author)
db.session.commit()
def login(self, username, password):
return self.app.post('/login', data=dict(
login=username,
password=password
), follow_redirects=True)
def logout(self):
return self.app.get('/logout', follow_redirects=True)
def test_login_form_present_for_guests(self):
rv = self.app.get('/')
assert 'Login' in str(rv.data)
def test_guest_can_create_account(self):
rv = self.app.post('/register', data=dict(
login='admin',
password='admin',
password_repeat='admin',
email='admin@mail.com'
), follow_redirects=True)
assert 'User sucsessfully registred' in str(rv.data)
def test_login_logout(self):
rv = self.login('admin', 'admin')
assert 'You were signed in' in str(rv.data)
rv = self.logout()
assert 'You were logged out' in str(rv.data)
rv = self.login('adminq', 'admin')
assert 'Incorrect login or password' in str(rv.data)
rv = self.login('admin', 'adminq')
assert 'Incorrect login or password' in str(rv.data)
def test_add_book(self):
self.login('admin', 'admin')
rv = self.app.post('/book_add', data=dict(
title='New Book',
author='auth1',
description='Nice book!'), follow_redirects=True)
assert 'Book added successfully' in str(rv.data)
self.logout()
rv = self.app.post('/book_add', data=dict(
title='New Book',
author='some aouthor',
description='test'), follow_redirects=True)
assert 'Please log in to access this page.' in str(rv.data)
def test_user_can_edit_book(self):
self.login('admin', 'admin')
self.create_book()
rv = self.app.post('/book_edit/1', data=dict(
title='New Title',
description='some new text',
author=1), follow_redirects=True)
assert 'Book has been updated' in str(rv.data)
def test_there_are_authors_in_book_edit_form(self):
self.login('admin', 'admin')
self.create_book()
rv = self.app.get('/book_edit/1', follow_redirects=True)
assert 'auth name' in str(rv.data)
def test_user_can_delete_book(self):
self.login('admin', 'admin')
self.create_book()
rv = self.app.get('/book_delete/1', follow_redirects=True)
assert 'Book has been deleted' in str(rv.data)
def test_user_can_add_author(self):
self.login('admin', 'admin')
rv = self.app.post('/author_add', data=dict(
name='Author Name'), follow_redirects=True)
assert 'Author added successfully' in str(rv.data)
def test_user_can_edit_author(self):
self.login('admin', 'admin')
self.create_author()
rv = self.app.post('/author_edit/1', data=dict(
name='New Name'), follow_redirects=True)
assert 'Author has been updated' in str(rv.data)
def test_user_can_delete_author(self):
self.login('admin', 'admin')
self.create_author()
rv = self.app.get('/author_delete/1', follow_redirects=True)
assert 'Author has been deleted' in str(rv.data)
def test_guest_can_find_the_book_by_title(self):
self.create_book()
rv = self.app.post('/search_results', data=dict(
search='New Book'), follow_redirects=True)
assert '>New Book</a>' in str(rv.data)
def test_guest_can_find_the_book_by_author(self):
self.create_author()
rv = self.app.post('/search_results', data=dict(
search='auth name'), follow_redirects=True)
assert '>auth name</a>' in str(rv.data)
if __name__ == '__main__':
unittest.main()