|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Tests of the OS X-specific functionality in certitude. These tests will only |
| 4 | +run on the OS X platform, and will otherwise be skipped. |
| 5 | +""" |
| 6 | +import os.path |
| 7 | +import platform |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +import certitude |
| 12 | +import certitude.osx |
| 13 | + |
| 14 | + |
| 15 | +is_osx = (platform.system() == 'Darwin') |
| 16 | + |
| 17 | + |
| 18 | +@pytest.mark.skipif(not is_osx, reason="OS X tests skipped on this platform") |
| 19 | +class TestOSX(object): |
| 20 | + def test_os_x_certificate_string_contains_pem_files(self): |
| 21 | + """ |
| 22 | + Calling the OS X-specific certificate_string function gives PEM files. |
| 23 | + """ |
| 24 | + data = certitude.osx.certificate_string() |
| 25 | + data.rstrip() |
| 26 | + assert data.startswith(b'-----BEGIN CERTIFICATE-----') |
| 27 | + assert data.endswith(b'-----END CERTIFICATE-----') |
| 28 | + |
| 29 | + def test_certificate_string_matches_osx_cert_string(self): |
| 30 | + """ |
| 31 | + The OS X-specific certificate_string function gives the same output as |
| 32 | + the "general" one. |
| 33 | + """ |
| 34 | + specific = certitude.osx.certificate_string() |
| 35 | + general = certitude.certificate_string() |
| 36 | + |
| 37 | + assert specific == general |
| 38 | + |
| 39 | + def test_certificate_file_contains_certificate_string(self): |
| 40 | + """ |
| 41 | + Confirm the certificate file written out is the same as the certificate |
| 42 | + string generated. |
| 43 | + """ |
| 44 | + with certitude.CertificateFile() as f: |
| 45 | + file_data = f.read() |
| 46 | + |
| 47 | + certstring = certitude.certificate_string() |
| 48 | + assert certstring == file_data |
| 49 | + |
| 50 | + def test_certificate_file_is_deleted_after_context_manager(self): |
| 51 | + """ |
| 52 | + The certificate file gets deleted after the context manager is exited. |
| 53 | + """ |
| 54 | + with certitude.CertificateFile() as f: |
| 55 | + path = f.path |
| 56 | + assert os.path.exists(path) |
| 57 | + |
| 58 | + assert not os.path.exists(path) |
| 59 | + |
| 60 | + def test_certificate_file_is_deleted_after_destroy(self): |
| 61 | + """ |
| 62 | + The certificate file gets deleted after the destroy method is called. |
| 63 | + """ |
| 64 | + f = certitude.CertificateFile() |
| 65 | + f.build() |
| 66 | + path = f.path |
| 67 | + assert os.path.exists(path) |
| 68 | + |
| 69 | + f.destroy() |
| 70 | + assert not os.path.exists(path) |
| 71 | + |
| 72 | + def test_cannot_read_destroyed_tempfile(self): |
| 73 | + """ |
| 74 | + Destroying a tempfile prevents reading from it. |
| 75 | + """ |
| 76 | + with certitude.CertificateFile() as f: |
| 77 | + assert len(f.read(1)) == 1 |
| 78 | + |
| 79 | + with pytest.raises(AttributeError): |
| 80 | + f.read() |
0 commit comments