-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
189 lines (113 loc) · 3.79 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
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
import os, sys
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
import unittest
import sqlalchemy
from flask_sqlalchemy import SQLAlchemy
from flask_blog import app, db
from flask import redirect, url_for
import pdb
from flask_blog.author.views import *
from flask_blog.blog.views import *
class UserTest(unittest.TestCase):
def setUp(self):
print('setUp()')
self.db_uri = 'mysql+pymysql://%s:%s@%s/' % ('harinder', '12345', 'localhost')
app.config['TESTING'] = True
app.config['WTF_CSRF_ENABLED'] = False
app.config['BLOG_DATABASE_NAME'] = 'test_blog'
app.config['SQLALCHEMY_DATABASE_URI'] = self.db_uri + app.config['BLOG_DATABASE_NAME']
engine = sqlalchemy.create_engine(self.db_uri)
conn = engine.connect()
conn.execute("commit")
conn.execute("create database " + app.config['BLOG_DATABASE_NAME'])
db.create_all()
conn.close()
self.app = app.test_client()
def tearDown(self):
print('tearDown()')
db.session.remove()
engine = sqlalchemy.create_engine(self.db_uri)
conn = engine.connect()
conn.execute("commit")
conn.execute("drop database " + app.config['BLOG_DATABASE_NAME'])
conn.close()
def create_blog(self):
return self.app.post('/setup',
data = dict(
blog_name = 'Test Blog',
author_name = 'Harinder',
email = 'harinder@gmail.com',
username = 'harinder',
password = '12345',
confirm = '12345'
)
,
follow_redirects = True
)
def login(self, username, password):
return self.app.post('/login', data = dict(
username = username,
password = password
),
follow_redirects = True)
def login_page_loads(self):
return self.app.get('/login')
def logout(self):
return self.app.get('/logout', follow_redirects = True)
def index(self):
return self.app.get('/', follow_redirects = True)
def post(self):
return self.app.post('/post',
data = dict(
title = 'Test Post',
body = 'Test Body',
category = 'Test category',
new_category = 'Test New Category',
slug = 'first-post'
),
follow_redirects = True
)
def update(self):
return self.app.post('/update')
def about(self):
return self.app.post('/about')
def contact_us(self):
return self.app.post('/contact_us', data = dict(
name = 'Harinder Singh',
email = 'hrinder@gmail.com',
subject = 'Test Subject',
message = 'Test Message',
),
follow_redirects = True)
def test_create_blog(self):
rv = self.create_blog()
assert "User Login" in str(rv.data)
def test_login_page_loads(self):
rv = self.login_page_loads()
assert "User Login" in str(rv.data)
def test_correct_login(self):
self.create_blog()
rv = self.login('harinder', '12345')
assert "Admin Options" in str(rv.data)
def test_incorrect_login(self):
self.create_blog()
rv = self.login('wrong', '12345')
assert "Login Failed" in str(rv.data)
def test_logout(self):
self.create_blog()
self.login('harinder', '12345')
rv = self.logout()
assert "User Login" in str(rv.data)
def test_index(self):
self.create_blog()
rv = self.index()
assert "Read Posts" in str(rv.data)
def test_about(self):
rv = self.about()
assert "Harinder Singh" in str(rv.data)
def test_contact_us(self):
rv = self.contact_us()
print(str(rv.data))
assert "Thanks" in str(rv.data)
if __name__ == '__main__':
unittest.main()