Newer
Older
import unittest
from os import environ
from app import app
from flask_testing import LiveServerTestCase
from json import loads
from database.database import db_session
from database.models import User, Role, Application
# Disable flask console log and output
environ['WERKZEUG_RUN_MAIN'] = 'true'
app.logger.disabled = True
log = logging.getLogger('werkzeug')
log.disabled = True
self.client = GraphQLClient(self.get_server_url()+"/graphql")
for username in ["graphqluser", "graphqluser2", "graphqluser3"]:
user = db_session.query(User).filter_by(username=username).first()
if user is not None:
user.roles = []
user.applications = []
db_session.delete(user)
db_session.commit()
role = db_session.query(Role).filter_by(name="testusers").first()
if role is not None:
role.users = []
db_session.delete(role)
db_session.commit()
application = db_session.query(Application).filter_by(name="testapp").first()
if application is not None:
application.users = []
db_session.delete(application)
db_session.commit()
username = "graphqluser"
password = "randompassword"
email = "user@example.com"
self.assertNotIn("error", result, "Creating user failed due to an error")
self.assertIn("data", result)
self.assertIn("createUser", result["data"])
self.assertIn("user", result["data"]["createUser"])
user = result["data"]["createUser"]["user"]
self.assertEqual(user["username"], username)
self.assertEqual(user["email"], email)
querystring = '''
{{
getUser(username: "{0}"){{
username,
email
}}
}}
'''.format(username).strip()
result = loads(self.client.execute(querystring))
self.assertIn("data", result)
self.assertIn("getUser", result["data"])
user = result["data"]["getUser"]
self.assertEqual(user["username"], username)
self.assertEqual(user["email"], email)
def test_verify_user_password(self):
username = "graphqluser"
password = "randompassword"
email = "user@example.com"
self._create_user(username, password, email)
querystring = '''
query{{
verifyPassword(
username: "{0}",
password: "{1}")
}}
'''.format(username, password).strip()
result = loads(self.client.execute(querystring))
self.assertIn("data", result)
self.assertTrue(result["data"]["verifyPassword"])
def _create_user(self, username, password, email):
querystring = '''
mutation{{
createUser(
username: "{0}",
password: "{1}",
email: "{2}"){{
user{{
username,
email}}}}}}
'''.format(username, password, email).strip()
return loads(self.client.execute(querystring))
role_name = "testusers"
role_description = "A role assigned to users during unittests"
result = self._create_role(role_name, role_description)
self.assertIn("data", result)
self.assertIn("createRole", result["data"])
self.assertIn("role", result["data"]["createRole"])
role = result["data"]["createRole"]["role"]
self.assertEqual(role["name"], role_name)
self.assertEqual(role["description"], role_description)
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
username = "graphqluser2"
password = "randompassword"
email = "user2@example.com"
self._create_user(username, password, email)
querystring = '''
mutation{{
addRoleToUser(
username: "{0}",
role: "{1}"){{
user{{
username,
roles{{
edges{{
node{{
name
}}
}}
}}
}}
role{{
name,
description
users{{
edges{{
node{{
username
}}
}}
}}
}}
}}
}}
'''.format(username, role_name).strip()
result = loads(self.client.execute(querystring))
self.assertIn("data", result)
self.assertIn("addRoleToUser", result["data"])
user = result["data"]["addRoleToUser"]["user"]
self.assertEqual(username, user["username"])
self.assertIn("edges", user["roles"])
roles = user["roles"]["edges"]
self.assertEqual(role_name, roles[0]["node"]["name"])
role = result["data"]["addRoleToUser"]["role"]
self.assertEqual(role_name, role["name"])
self.assertIn("edges", role["users"])
users = role["users"]["edges"]
self.assertEqual(username, users[0]["node"]["username"])
def _create_role(self, name, description):
querystring = '''
mutation{{
createRole(
name: "{0}",
description: "{1}"){{
role{{
name,
description}}}}}}
'''.format(name, description).strip()
return loads(self.client.execute(querystring))
def test_create_and_assign_application(self):
application_name = "testapp"
result = self._create_application(application_name)
self.assertIn("data", result)
self.assertIn("createApplication", result["data"])
self.assertIn("application", result["data"]["createApplication"])
application = result["data"]["createApplication"]["application"]
self.assertEqual(application["name"], application_name)
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
username = "graphqluser3"
password = "randompassword"
email = "user3@example.com"
self._create_user(username, password, email)
querystring = '''
mutation{{
addApplicationToUser(
username: "{0}",
application: "{1}"){{
user{{
username,
applications{{
edges{{
node{{
name
}}
}}
}}
}}
application{{
name,
users{{
edges{{
node{{
username
}}
}}
}}
}}
}}
}}
'''.format(username, application_name).strip()
result = loads(self.client.execute(querystring))
self.assertIn("data", result)
self.assertIn("addApplicationToUser", result["data"])
user = result["data"]["addApplicationToUser"]["user"]
self.assertEqual(username, user["username"])
self.assertIn("edges", user["applications"])
applications = user["applications"]["edges"]
self.assertEqual(application_name, applications[0]["node"]["name"])
application = result["data"]["addApplicationToUser"]["application"]
self.assertEqual(application_name, application["name"])
self.assertIn("edges", application["users"])
users = application["users"]["edges"]
self.assertEqual(username, users[0]["node"]["username"])