Skip to content

Commit 8decd83

Browse files
authored
Merge pull request #868 from helgi/test_root
ref(tests): expose TEST_ROOT in test for file based (certs, etc) access
2 parents 455f9c9 + 57e7f16 commit 8decd83

9 files changed

Lines changed: 22 additions & 37 deletions

rootfs/api/tests/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import random
33
import requests_mock
44
import time
5+
from os.path import dirname, realpath
56

67
from django.conf import settings
78
from django.test.runner import DiscoverRunner
@@ -39,6 +40,9 @@ def fake_responses(request, context):
3940
adapter.register_uri('GET', url + '/health', text=fake_responses)
4041
adapter.register_uri('GET', url + '/healthz', text=fake_responses)
4142

43+
# Root of the test directory (for files and such)
44+
TEST_ROOT = dirname(realpath(__file__))
45+
4246

4347
class SilentDjangoTestSuiteRunner(DiscoverRunner):
4448
"""Prevents api log messages from cluttering the console during tests."""

rootfs/api/tests/test_build.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
44
Run the tests with "./manage.py test api"
55
"""
6-
7-
86
import json
97

108
from django.contrib.auth.models import User

rootfs/api/tests/test_certificate.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
1-
import os
2-
31
from django.contrib.auth.models import User
42
from django.core.cache import cache
53
from rest_framework.test import APITestCase
64
from rest_framework.authtoken.models import Token
75
from django.core.exceptions import SuspiciousOperation
86

97
from api.models import App, Certificate
8+
from api.tests import TEST_ROOT
109

1110

1211
class CertificateTest(APITestCase):
@@ -24,11 +23,10 @@ def setUp(self):
2423
self.app = App.objects.create(owner=self.user, id='test-app')
2524
self.domain = 'autotest.example.com'
2625

27-
path = os.path.dirname(os.path.realpath(__file__))
28-
with open('{}/certs/{}.key'.format(path, self.domain)) as f:
26+
with open('{}/certs/{}.key'.format(TEST_ROOT, self.domain)) as f:
2927
self.key = f.read()
3028

31-
with open('{}/certs/{}.cert'.format(path, self.domain)) as f:
29+
with open('{}/certs/{}.cert'.format(TEST_ROOT, self.domain)) as f:
3230
self.cert = f.read()
3331

3432
def tearDown(self):

rootfs/api/tests/test_certificate_use_case_1.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import os
2-
31
from django.contrib.auth.models import User
42
from django.core.cache import cache
53
from rest_framework.test import APITestCase
64
from rest_framework.authtoken.models import Token
75

86
from api.models import App, Certificate, Domain
7+
from api.tests import TEST_ROOT
98

109

1110
class CertificateUseCase1Test(APITestCase):
@@ -27,11 +26,10 @@ def setUp(self):
2726
self.domain = Domain.objects.create(owner=self.user, app=self.app, domain='foo.com')
2827
self.name = 'foo-com' # certificate name
2928

30-
path = os.path.dirname(os.path.realpath(__file__))
31-
with open('{}/certs/{}.key'.format(path, self.domain)) as f:
29+
with open('{}/certs/{}.key'.format(TEST_ROOT, self.domain)) as f:
3230
self.key = f.read()
3331

34-
with open('{}/certs/{}.cert'.format(path, self.domain)) as f:
32+
with open('{}/certs/{}.cert'.format(TEST_ROOT, self.domain)) as f:
3533
self.cert = f.read()
3634

3735
def tearDown(self):

rootfs/api/tests/test_certificate_use_case_2.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import os
2-
31
from django.contrib.auth.models import User
42
from django.core.cache import cache
53
from rest_framework.test import APITestCase
64
from rest_framework.authtoken.models import Token
75

86
from api.models import App, Certificate, Domain
7+
from api.tests import TEST_ROOT
98

109

1110
class CertificateUseCase2Test(APITestCase):
@@ -32,12 +31,11 @@ def setUp(self):
3231
# only foo.com has a cert
3332
self.domain = 'foo.com'
3433

35-
path = os.path.dirname(os.path.realpath(__file__))
3634
self.certificates = {self.domain: {'name': self.domain.replace('.', '-')}}
37-
with open('{}/certs/{}.key'.format(path, self.domain)) as f:
35+
with open('{}/certs/{}.key'.format(TEST_ROOT, self.domain)) as f:
3836
self.certificates[self.domain]['key'] = f.read()
3937

40-
with open('{}/certs/{}.cert'.format(path, self.domain)) as f:
38+
with open('{}/certs/{}.cert'.format(TEST_ROOT, self.domain)) as f:
4139
self.certificates[self.domain]['cert'] = f.read()
4240

4341
# add expires and fingerprints

rootfs/api/tests/test_certificate_use_case_3.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import os
2-
31
from django.contrib.auth.models import User
42
from django.core.cache import cache
53
from rest_framework.test import APITestCase
64
from rest_framework.authtoken.models import Token
75

86
from api.models import App, Certificate, Domain
7+
from api.tests import TEST_ROOT
98

109

1110
class CertificateUseCase3Test(APITestCase):
@@ -31,15 +30,13 @@ def setUp(self):
3130

3231
self.certificates = {}
3332

34-
path = os.path.dirname(os.path.realpath(__file__))
35-
3633
# load up the certs
3734
for domain in self.domains:
3835
self.certificates[domain] = {'name': domain.replace('.', '-')}
39-
with open('{}/certs/{}.key'.format(path, domain)) as f:
36+
with open('{}/certs/{}.key'.format(TEST_ROOT, domain)) as f:
4037
self.certificates[domain]['key'] = f.read()
4138

42-
with open('{}/certs/{}.cert'.format(path, domain)) as f:
39+
with open('{}/certs/{}.cert'.format(TEST_ROOT, domain)) as f:
4340
self.certificates[domain]['cert'] = f.read()
4441

4542
# add expires and fingerprints

rootfs/api/tests/test_certificate_use_case_4.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import os
2-
31
from django.contrib.auth.models import User
42
from django.core.cache import cache
53
from rest_framework.test import APITestCase
64
from rest_framework.authtoken.models import Token
75

86
from api.models import App, Certificate, Domain
7+
from api.tests import TEST_ROOT
98

109

1110
class CertificateUseCase4Test(APITestCase):
@@ -32,8 +31,6 @@ def setUp(self):
3231

3332
self.certificates = {}
3433

35-
path = os.path.dirname(os.path.realpath(__file__))
36-
3734
# load up the certs
3835
for domain in self.domains:
3936
self.certificates[domain] = {'name': domain.replace('.', '-').replace('*', 'wildcard')}
@@ -42,10 +39,10 @@ def setUp(self):
4239
# Cheap hack
4340
filename = domain.replace('*', 'www')
4441

45-
with open('{}/certs/{}.key'.format(path, filename)) as f:
42+
with open('{}/certs/{}.key'.format(TEST_ROOT, filename)) as f:
4643
self.certificates[domain]['key'] = f.read()
4744

48-
with open('{}/certs/{}.cert'.format(path, filename)) as f:
45+
with open('{}/certs/{}.cert'.format(TEST_ROOT, filename)) as f:
4946
self.certificates[domain]['cert'] = f.read()
5047

5148
# add expires, common_name and fingerprints

rootfs/api/tests/test_certificate_use_case_5.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import os
2-
31
from django.contrib.auth.models import User
42
from django.core.cache import cache
53
from rest_framework.test import APITestCase
64
from rest_framework.authtoken.models import Token
75

86
from api.models import App, Certificate, Domain
7+
from api.tests import TEST_ROOT
98

109

1110
class CertificateUseCase5Test(APITestCase):
@@ -33,17 +32,15 @@ def setUp(self):
3332

3433
self.certificates = {}
3534

36-
path = os.path.dirname(os.path.realpath(__file__))
37-
3835
# load up the certs
3936
for domain in self.domains:
4037
self.certificates[domain] = {'name': domain.replace('.', '-').replace('*', 'wildcard')}
4138
filename = domain.replace('*', 'wildcard')
4239

43-
with open('{}/certs/{}.key'.format(path, filename)) as f:
40+
with open('{}/certs/{}.key'.format(TEST_ROOT, filename)) as f:
4441
self.certificates[domain]['key'] = f.read()
4542

46-
with open('{}/certs/{}.cert'.format(path, filename)) as f:
43+
with open('{}/certs/{}.cert'.format(TEST_ROOT, filename)) as f:
4744
self.certificates[domain]['cert'] = f.read()
4845

4946
# add expires, common_name and fingerprints

rootfs/api/tests/test_pods.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
44
Run the tests with "./manage.py test api"
55
"""
6-
7-
86
import json
97

108
from django.contrib.auth.models import User

0 commit comments

Comments
 (0)