def test_get_credentials_missing_section(self): """ Test reading the credentials from a configuration file where the credentials section is missing (uninitialized). """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" username, password = ("*****@*****.**", "password") self.mox.StubOutWithMock(SafeConfigParser, "get") self.mox.StubOutWithMock(SafeConfigParser, "add_section") self.mox.StubOutWithMock(SafeConfigParser, "set") self.mox.StubOutWithMock(__builtin__, "raw_input") config = ConfigurationParser() config.parse(mock_file) error = NoSectionError("credentials") config.get("credentials", "username").AndRaise(error) config.add_section("credentials") raw_input(mox.IgnoreArg()).AndReturn(username) raw_input(mox.IgnoreArg()).AndReturn(password) config.set("credentials", "username", username) config.set("credentials", "password", password) self.mox.ReplayAll() self.assertEquals((username, password), credentials.get_credentials())
def test_adding_section(self): """ Test adding a section - this should be a simple delegate to SafeConfigParser.add_section, with a write upon successful set. """ mock_file = self.mox.CreateMockAnything() mock_file.closed = False mock_file.name = "foobar" self.mox.StubOutWithMock(SafeConfigParser, "read") self.mox.StubOutWithMock(SafeConfigParser, "add_section") self.mox.StubOutWithMock(SafeConfigParser, "write") SafeConfigParser.read(mock_file.name) SafeConfigParser.add_section("credentials") mock_file.truncate(0) mock_file.flush() SafeConfigParser.write(mock_file) self.mox.ReplayAll() config = ConfigurationParser() config.parse(mock_file) config.add_section("credentials")