def test_should_return_all_servers(self): self.config.save_server_config(ServerConfig('url1', [], 'EDT', 'prefix', 'user', 'pass')) self.config.save_server_config(ServerConfig('url2', [], 'EDT', 'prefix', 'user', 'pass')) servers = self.config.get_server_configs() self.assertEquals(2, len(servers)) self.assertEquals('http://url1', servers[0].url) self.assertEquals('http://url2', servers[1].url)
def get_server_config(self, url): username = self.get_username(url) return ServerConfig(url, self.get_project_excludes(url), self.get_timezone(url), self.get_display_prefix(url), username, self.get_password(url, username), self.get_skip_ssl_verification(url))
def test_should_fetch_data_without_auth(mocker): m = mocker.patch.object(urllib2, 'urlopen', return_value='content') response = HttpConnection().connect(ServerConfig('localhost:8080/cc.xml', [], '', '', None, None), 3) assert str(response) == 'content' def request_matcher(a): assert a.get_full_url() == 'http://localhost:8080/cc.xml' and a.get_header('Authorization') is None return True m.assert_called_once_with(Matcher(request_matcher))
def test_should_pass_auth_if_provided(mocker): m = mocker.patch.object(urllib2, 'urlopen', return_value='content') response = HttpConnection().connect(ServerConfig('http://localhost:8080/cc.xml', [], '', '', 'user', 'pass'), 3) assert str(response) == 'content' def url_with_auth(a): assert a.get_full_url() == 'http://localhost:8080/cc.xml' and a.get_header('Authorization') is not None return True m.assert_called_once_with(Matcher(url_with_auth))
def __init__(self, server_url, prefix, timezone, props): self.server_url = server_url self.prefix = prefix self.timezone = timezone self.name = props['name'] self.status = props['lastBuildStatus'] self.activity = props['activity'] self.url = ServerConfig.cleanup(props['url']) self.last_build_time = props['lastBuildTime'] self.last_build_label = props.get('lastBuildLabel', None)
def test_should_respond_even_if_things_fail(self): class MockConnection(object): def __init__(self): pass def connect(self, server, timeout): raise Exception("something went wrong") response = ProjectLoader(ServerConfig('url', [], '', '', '', ''), 10, MockConnection()).get_data() projects = response.server.get_projects() self.assertEquals(0, len(projects)) self.assertEquals(True, response.server.unavailable)
def test_should_store_server_preferences(self): self.config.save_server_config( ServerConfig('https://github.com/anaynayak/buildnotify/cctray.xml', ['excludedproject'], 'EDT', 'prefix', 'user', 'pass')) server = self.config.get_server_config("https://github.com/anaynayak/buildnotify/cctray.xml") self.assertEquals('user', server.username) self.assertEquals('pass', server.password) self.assertEquals("https://github.com/anaynayak/buildnotify/cctray.xml", server.url) self.assertEquals('EDT', server.timezone) self.assertEquals(['excludedproject'], server.excluded_projects) self.assertEquals('prefix', server.prefix) self.assertEquals(["https://github.com/anaynayak/buildnotify/cctray.xml"], self.config.get_urls())
def test_should_set_display_prefix(self): connection = MockConnection(lambda: StringIO("""<?xml version="1.0" encoding="UTF-8"?> <Projects> <Project name="project" activity="Sleeping" lastBuildStatus="Success" lastBuildTime="2009-06-12T06:54:35" webUrl="http://local/url"/> </Projects>""")) response = ProjectLoader(ServerConfig('url', [], '', 'RELEASE', '', ''), 10, connection).get_data() projects = response.server.get_projects() self.assertEquals(1, len(projects)) self.assertEquals("[RELEASE] project", projects[0].label())
def test_should_load_feed(self): connection = MockConnection(lambda: StringIO("""<?xml version="1.0" encoding="UTF-8"?> <Projects> <Project name="project" activity="Sleeping" lastBuildStatus="Success" lastBuildTime="2009-06-12T06:54:35" webUrl="http://local/url"/> </Projects>""")) response = ProjectLoader(ServerConfig('url', [], '', '', '', ''), 10, connection).get_data() projects = response.server.get_projects() self.assertEquals(1, len(projects)) self.assertEquals("project", projects[0].name) self.assertEquals("Sleeping", projects[0].activity) self.assertEquals("Success", projects[0].status) self.assertEquals(False, response.server.unavailable)
def connect(self, server: ServerConfig, timeout: Optional[float], additional_headers: Dict[str, str] = None) -> str: headers = {'user-agent': self.user_agent} headers.update(additional_headers or {}) auth = (server.username, server.password) if server.has_creds() else None response = requests.get(server.url, verify=not server.skip_ssl_verification, headers=headers, auth=auth, timeout=timeout) response.encoding = 'utf-8' response.raise_for_status() return response.text
def __init__(self, url: Optional[str], conf: Config, parent: QWidget = None): QDialog.__init__(self, parent) self.ui = Ui_serverConfigurationDialog() self.ui.setupUi(self) self.conf = conf self.projects_list = QtGui.QStandardItem("All") all_timezones = [Config.NONE_TIMEZONE] all_timezones.extend(pytz.all_timezones) self.ui.timezoneList.addItems(all_timezones) if url is not None: self.ui.addServerUrl.setText(url) self.server = conf.get_server_config(url) self.ui.timezoneList.setCurrentIndex( all_timezones.index(self.server.timezone)) self.ui.displayPrefix.setText(self.server.prefix) self.ui.username.setText(self.server.username) self.ui.password.setText(self.server.password) self.ui.authentication_type.setCurrentIndex( self.server.authentication_type) self.ui.usernameLabel.setVisible( self.server.authentication_type == self.server.AUTH_USERNAME_PASSWORD) self.ui.username.setVisible(self.server.authentication_type == self.server.AUTH_USERNAME_PASSWORD) else: self.server = ServerConfig('', [], '', '', '', '') self.ui.loadUrlButton.clicked.connect(self.fetch_data) if not Keystore.is_available(): self.ui.authenticationSettings.setTitle( 'Authentication (keyring dependency missing)') self.ui.authentication_type.setEnabled(False) self.ui.username.setEnabled(False) self.ui.password.setEnabled(False) self.ui.authentication_type.currentIndexChanged.connect( self.set_authentication_type) self.ui.backButton.clicked.connect( lambda: self.ui.stackedWidget.setCurrentIndex(0)) self.skip_ssl_verification = False
def test_should_cleanup_url(): assert ServerConfig('http://url.com:9800/cc.xml', [], '', '', '', '').url == 'http://url.com:9800/cc.xml' assert ServerConfig('url.com:9800/cc.xml', [], '', '', '', '').url == 'http://url.com:9800/cc.xml' assert ServerConfig('url.com/cc.xml', [], '', '', '', '').url == 'http://url.com/cc.xml' assert ServerConfig('localhost:8080/cc.xml', [], '', '', '', '').url == 'http://localhost:8080/cc.xml' assert ServerConfig('http://localhost:8080/cc.xml', [], '', '', '', '').url == 'http://localhost:8080/cc.xml' assert ServerConfig('https://localhost:8080/cc.xml', [], '', '', '', '').url == 'https://localhost:8080/cc.xml' assert ServerConfig('file://localhost:8080/cc.xml', [], '', '', '', '').url == 'file://localhost:8080/cc.xml'
def get_server_config(self): projects_model = self.ui.projectsList.model() def project(i, model): return model.index(i, 0, self.parent.index()) excluded_projects = [ project(i, projects_model).data().encode('utf-8') for i in range(self.parent.rowCount()) if project( i, projects_model).data(Qt.CheckStateRole) == Qt.Unchecked ] return ServerConfig(self.server_url(), excluded_projects, str(self.ui.timezoneList.currentText()), self.ui.displayPrefix.text().encode('utf-8'), str(self.ui.username.text()), str(self.ui.password.text()), self.skip_ssl_verification)
def test_should_fetch_data(): file_path = os.path.realpath(os.path.dirname(os.path.abspath(__file__)) + "../../data/cctray.xml") response = HttpConnection().connect(ServerConfig('file://' + file_path, [], '', '', None, None), 3) assert str(response.read()) == open(file_path, 'r').read()