From 47f15eca6c3cbabe3de608512a8583022218f887 Mon Sep 17 00:00:00 2001 From: Ken Dreyer Date: Sep 28 2017 20:31:16 +0000 Subject: add tests Add some simple tests for the download_cert() method. --- diff --git a/SOURCES/test_centos_cert.py b/SOURCES/test_centos_cert.py new file mode 100644 index 0000000..d2dfd00 --- /dev/null +++ b/SOURCES/test_centos_cert.py @@ -0,0 +1,64 @@ +import imp +import pytest +import requests + +centos_cert = imp.load_source('centos_cert', 'centos-cert') + + +class MockResponse(object): + def raise_for_status(self): + pass + + @property + def text(self): + if self.url == 'https://accounts.centos.org/user/dogencert': + return 'DUMMY-USER-CERT' + if self.url == 'https://accounts.centos.org/ca/ca-cert.pem': + return 'DUMMY-CA-CERT' + raise NotImplementedError() + + +class RequestRecorder(object): + """ Record args to requests.get() or requests.post() """ + def __call__(self, url, **kwargs): + """ mocking requests.get() or requests.post() """ + self.response = MockResponse() + self.response.url = url + self.kwargs = kwargs + return self.response + + +@pytest.fixture +def mock_get(): + return RequestRecorder() + + +@pytest.fixture +def mock_post(): + return RequestRecorder() + + +def test_download(monkeypatch, tmpdir, mock_get, mock_post): + monkeypatch.delattr('requests.sessions.Session.request') + monkeypatch.setenv('HOME', str(tmpdir)) + monkeypatch.setattr(requests, 'get', mock_get) + monkeypatch.setattr(requests, 'post', mock_post) + centos_cert.download_cert('myuser', 'mypass') + + assert mock_get.response.url == \ + 'https://accounts.centos.org/ca/ca-cert.pem' + assert mock_post.response.url == \ + 'https://accounts.centos.org/user/dogencert' + + certfile = tmpdir.join('.centos.cert') + assert certfile.check(file=1) + assert certfile.read() == 'DUMMY-USER-CERT' + assert oct(certfile.stat().mode & 0777) == '0600' + + cacertfile = tmpdir.join('.centos-server-ca.cert') + assert cacertfile.check(file=1) + assert cacertfile.read() == 'DUMMY-CA-CERT' + + uploadcafile = tmpdir.join('.centos-upload-ca.cert') + assert uploadcafile.check(file=1) + assert uploadcafile.read() == 'DUMMY-CA-CERT'