1
+ """Contains base test case with needed setup and helpers."""
2
+
1
3
import os
2
4
from collections import namedtuple
3
5
from contextlib import contextmanager
22
24
23
25
@contextmanager
24
26
def provide_file_at_root (file_name , to_write = None ):
25
- """
26
- Provide file with name file_name at application root.
27
- """
27
+ """Provide file with name file_name at application root."""
28
28
if to_write is None :
29
29
to_write = "DATABASE_URI = 'sqlite:///:memory:'"
30
30
@@ -48,9 +48,7 @@ def load_file_lines(filepath):
48
48
49
49
50
50
def mock_decorator (f ):
51
- """
52
- Mock login_required decorator.
53
- """
51
+ """Mock login_required decorator."""
54
52
@wraps (f )
55
53
def decorated_function (* args , ** kwargs ):
56
54
return f (* args , ** kwargs )
@@ -59,6 +57,7 @@ def decorated_function(*args, **kwargs):
59
57
60
58
61
59
def generate_keys ():
60
+ """Generate CSRF session and secret keys."""
62
61
from utility import ROOT_DIR
63
62
secret_csrf_path = f"{ os .path .join (ROOT_DIR , '' )} secret_csrf"
64
63
secret_key_path = f"{ os .path .join (ROOT_DIR , '' )} secret_key"
@@ -73,6 +72,7 @@ def generate_keys():
73
72
74
73
75
74
def load_config (file ):
75
+ """Load start config."""
76
76
key_paths = generate_keys ()
77
77
with open (key_paths ['secret_key_path' ], 'rb' ) as secret_key_file :
78
78
secret_key = secret_key_file .read ()
@@ -102,6 +102,7 @@ def load_config(file):
102
102
103
103
104
104
def mock_api_request_github (url , data = None , timeout = None ):
105
+ """Mock all responses to the Github API."""
105
106
if url == "https://api.github.com/repos/test/test_repo/commits/abcdef" :
106
107
return MockResponse ({}, 200 )
107
108
elif url == "https://api.github.com/user" :
@@ -139,7 +140,7 @@ def mock_api_request_github(url, data=None, timeout=None):
139
140
140
141
def generate_signature (data , private_key ):
141
142
"""
142
- Generate signature token of hook request
143
+ Generate signature token of hook request.
143
144
144
145
:param data: Signature's data
145
146
:param private_key: Signature's token
@@ -171,19 +172,19 @@ def generate_git_api_header(event, sig):
171
172
172
173
173
174
class BaseTestCase (TestCase ):
175
+ """Base setup for all test cases."""
176
+
174
177
@mock .patch ('config_parser.parse_config' , side_effect = load_config )
175
178
def create_app (self , mock_config ):
176
- """
177
- Create an instance of the app with the testing configuration
178
- :return:
179
- """
179
+ """Create an instance of the app with the testing configuration."""
180
180
user = namedtuple ('user' , "name password email github_token" )
181
181
self .user = user (name = "test" , password = "test123" ,
182
182
email = "[email protected] " ,
github_token = "abcdefgh" )
183
183
from run import app
184
184
return app
185
185
186
186
def setUp (self ):
187
+ """Set up all entities."""
187
188
self .app .preprocess_request ()
188
189
g .db = create_session (
189
190
self .app .config ['DATABASE_URI' ], drop_tables = True )
@@ -304,13 +305,15 @@ def setUp(self):
304
305
@staticmethod
305
306
def create_login_form_data (email , password ) -> dict :
306
307
"""
307
- Creates the form data for a login event.
308
+ Create the form data for a login event.
309
+
308
310
:return: A dictionary containing the name, password and submit fields.
309
311
"""
310
312
return {'email' : email , 'password' : password , 'submit' : True }
311
313
312
314
@staticmethod
313
315
def create_customize_form (commit_hash , platform , commit_select = None , regression_test = None ):
316
+ """Create the request form part."""
314
317
if regression_test is None :
315
318
regression_test = [1 , 2 ]
316
319
if commit_select is None :
@@ -324,9 +327,7 @@ def create_customize_form(commit_hash, platform, commit_select=None, regression_
324
327
}
325
328
326
329
def create_forktest (self , commit_hash , platform , regression_tests = None ):
327
- """
328
- Create a test on fork based on commit and platform
329
- """
330
+ """Create a test on fork based on commit and platform."""
330
331
from flask import g
331
332
fork_url = f"https://github.com/{ self .user .name } /{ g .github ['repository' ]} .git"
332
333
fork = Fork (fork_url )
@@ -346,9 +347,7 @@ def create_forktest(self, commit_hash, platform, regression_tests=None):
346
347
g .db .commit ()
347
348
348
349
def create_user_with_role (self , user , email , password , role , github_token = None ):
349
- """
350
- Create a user with specified user details and role.
351
- """
350
+ """Create a user with specified user details and role."""
352
351
from flask import g
353
352
user = User (self .user .name , email = self .user .email ,
354
353
password = User .generate_hash (self .user .password ), role = role , github_token = github_token )
@@ -357,16 +356,20 @@ def create_user_with_role(self, user, email, password, role, github_token=None):
357
356
358
357
@staticmethod
359
358
def create_random_string (length = 32 ):
359
+ """Generate random string of ASCII symbols."""
360
360
import random
361
361
import string
362
362
random_string = '' .join ([random .choice (string .ascii_letters + string .digits ) for n in range (length )])
363
363
return random_string
364
364
365
365
366
366
class MockResponse :
367
+ """A class to mock HTTP response."""
368
+
367
369
def __init__ (self , json_data , status_code ):
368
370
self .json_data = json_data
369
371
self .status_code = status_code
370
372
371
373
def json (self ):
374
+ """Mock response json method."""
372
375
return self .json_data
0 commit comments