Ejemplo n.º 1
0
class ConfigDbTestCase(unittest.TestCase):

    def setUp(self):
        self.dbn = DBNAME
        self.tearDown()
        self.cfgdb = ConfigDb(self.dbn)

    def tearDown(self):        
        if os.path.exists(self.dbn):
            os.remove(self.dbn)

    def test_init(self):
        '''Test the ConfigDb() or __init__ . 
        After called this function, it should exists a default target record.
        '''
        with offtheshelf.openDB(self.dbn) as db:
            targets = db.get_collection("targets")
            x = targets.find({'name': 'local', 'default': True, 'url': DefaultTarget})
            assert x != None
            eq_(1, len(x))

    def test_get_targets(self):
        ts = self.cfgdb.get_targets()
        eq_(1, len(ts))
        t = ts[0]
        eq_('local', t['name'])
        eq_(True, t['default'])
        eq_(DefaultTarget, t['url'])

    def test_get_default_target(self):
        t = self.cfgdb.get_default_target()
        assert t != None
        eq_('local', t['name'])
        eq_(True, t['default'])
        eq_(DefaultTarget, t['url'])

    def test_set_default_target(self):
        self.cfgdb.add_target("test", "http://*****:*****@gmail.com", "token", True)
        x = self.cfgdb.get_default_user()
        assert_not_equal(None, x)
        eq_('tester', x['name'])
        eq_('*****@*****.**', x['email'])

    def test_remove_user(self):
        pass

    def test_x(self):
        #assert_equal(1, "Not Implemented")
        pass
Ejemplo n.º 2
0
 def setUp(self):
     self.dbn = DBNAME
     self.tearDown()
     self.cfgdb = ConfigDb(self.dbn)
Ejemplo n.º 3
0
class TestAppsTestCase(unittest.TestCase):

    def setUp(self):
        self.dbn = DBNAME
        self.tearDown()
        self.cfgdb = ConfigDb(self.dbn)

    def tearDown(self):
        if os.path.exists(self.dbn):
            os.remove(self.dbn)        

    # now @with_setup(loggedin) can not working?
    # nose can not working with unittest.Testcase or it's subclass
    def loggedin(self):
        self.cfgdb.add_user("test", "*****@*****.**", "token", True)       
    
    @mock.patch("requests.get")
    def test_ListAppsWithLogin(self, get):
        t = self.cfgdb.get_default_target()
        eq_(t['name'], 'local')
        eq_(t['url'], 'http://127.0.0.1:8080')
        self.loggedin()
        am = AppManager("target", self.dbn)
        am.list()
        get.assert_called_with("target/apps", headers=TestAuthHeader)

    @mock.patch("requests.get")
    def test_ListAppsWithoutLogin(self, get):
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.list()
        out, err = capture.reset()
        get.not_called()
        self.assertEquals(out.strip(), ELoginFirst)

    @mock.patch("requests.get")
    def test_GetAppWithLogin(self, get):
        self.loggedin()
        am = AppManager("target", self.dbn)
        am.get("myapp")
        get.assert_called_with("target/apps/myapp", headers=TestAuthHeader)

    @mock.patch("requests.get")
    def test_GetAppWithoutLogin(self, get):
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.get("myapp")
        out, err = capture.reset()
        get.not_called()
        self.assertEquals(out.strip(), ELoginFirst)

    @mock.patch("requests.delete")
    def test_RemoveAppWithLogin(self, delete):
        self.loggedin()
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.remove("myapp")
        out, err = capture.reset()
        delete.assert_called_with("target/apps/myapp", headers=TestAuthHeader)
        self.assertEquals(out.strip(), IRemoveApp)

    @mock.patch("requests.delete")
    def test_RemoveAppWithoutLogin(self, delete):
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.remove("myapp")
        out, err = capture.reset()
        delete.not_called()
        self.assertEquals(out.strip(), ELoginFirst)

    @mock.patch("requests.post")
    def test_CreateAppWithLogin(self, post):
        self.loggedin()
        am = AppManager("target", self.dbn)
        data = {
            "name": "myapp",
            "framework": "python2.7",
            "units": 5
        }
        b = json.dumps(data)
        capture = py.io.StdCaptureFD(in_=False)
        am.create("myapp", "python2.7", 5)
        out, err = capture.reset()
        post.assert_called_with("target/apps", data=b, headers=TestAuthHeader)
        self.assertEquals(out.strip(), ICreateApp)

    @mock.patch("requests.post")
    def test_CreateAppWithoutLogin(self, post):
        am = AppManager("target", self.dbn)
        data = {
            "name": "myapp",
            "framework": "python2.7",
            "units": 5
        }
        b = json.dumps(data)
        capture = py.io.StdCaptureFD(in_=False)
        am.create("myapp", "python2.7", 5)
        out, err = capture.reset()
        post.not_called()
        self.assertEquals(out.strip(), ELoginFirst)

    @mock.patch("requests.put")
    def test_AddUnitWithLogin(self, put):
        self.loggedin()
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.unitadd("myapp", 5)
        out, err = capture.reset()
        put.assert_called_with("target/apps/myapp/units", data=str(5), headers=TestAuthHeader)
        self.assertEquals(out.strip(), IAddUnit)

    @mock.patch("requests.put")
    def test_AddUnitWithoutLogin(self, put):
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.unitadd("myapp", 5)
        out, err = capture.reset()
        put.not_called()
        self.assertEquals(out.strip(), ELoginFirst)

    @mock.patch("requests.delete")
    def test_RemoveUnitWithLogin(self, delete):
        self.loggedin()
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.unitremove("myapp", 5)
        out, err = capture.reset()
        delete.assert_called_with("target/apps/myapp/units", data=str(5), headers=TestAuthHeader)
        self.assertEquals(out.strip(), IRemoveUnit)

    @mock.patch("requests.delete")
    def test_RemoveUnitWithoutLogin(self, delete):
        am = AppManager("target", self.dbn)
        capture = py.io.StdCaptureFD(in_=False)
        am.unitremove("myapp", 5)
        out, err = capture.reset()
        #out, err = capsys.readouterr()
        delete.not_called()
        self.assertEquals(out.strip(), ELoginFirst)