def add_target(self, name, url, is_default=False): with offtheshelf.openDB(self.dbname) as db: tcoll = db.get_collection("targets") tcoll.upsert({'name':name, 'url': url, 'default': False}, {'name': name}) if is_default: self.set_default_target(name)
def _insertInitData(self): with offtheshelf.openDB(self.dbname) as db: targets = db.get_collection("targets") targets.insert({"name": "local", "url": DefaultTarget, "default": True}) return
def set_default_target(self, name): with offtheshelf.openDB(self.dbname) as db: tcoll = db.get_collection("targets") # clear the old default target tcoll.update({'default': False}) # set the new default target tcoll.update({'default': True}, {'name': name.lower()})
def test_LoginSucessWithLoginAndYesConfirm(post, getpass, raw_input, capsys): class Response: """It is the post()'s return value""" def __init__(self): self.ok = True def json(self): return {"token": "token"} loggedin() data = {"password": TestPassword} am = AuthManager("target", dbn) post.return_value = Response() am.login(TestUser, TestEmail2) out, err = capsys.readouterr() raw_input.assert_called_with(QHaveLoggedIn) post.assert_called_with("target/users/{0}/tokens".format(TestEmail2), data=json.dumps(data)) assert (out.strip(), ILogin) # confirm the cfgdb's content with offtheshelf.openDB(dbn) as db: users = db.get_collection("users") x = users.find({"name": TestUser, "email": TestEmail2, "default": True}) assert x != None assert 1 == len(x)
def get_default_target(self): with offtheshelf.openDB(self.dbname) as db: tcoll = db.get_collection("targets") x = tcoll.find({'default': True}) if len(x) > 0: return x[0] else: return None
def get_target(self, name): with offtheshelf.openDB(self.dbname) as db: coll = db.get_collection("targets") x = coll.find({"name": name}) if len(x) > 0: return x[0] else: return None
def is_user_loggedin(self, name): with offtheshelf.openDB(self.dbname) as db: coll = db.get_collection("users") x = coll.find({'name': name}) if x and len(x) > 0 and x[0]['token'] and len(x[0]['token'])>0: return True else: return False
def readToken(dbn): with offtheshelf.openDB(dbn) as db: coll = db.get_collection("users") x = coll.find({'default': True}) if x and len(x) > 0 and x[0]['token']: return x[0]['token'] else: return None
def add_user(self, name, email, token=None, is_default=False): '''Add or update a user's info. ''' with offtheshelf.openDB(self.dbname) as db: coll = db.get_collection("users") coll.upsert({'name':name, 'email': email, 'default': False, 'token': token}, {'name': name}) if is_default: self.set_default_user(name)
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 remove_target(self, name): with offtheshelf.openDB(self.dbname) as db: tcoll = db.get_collection("targets") if len(tcoll.find()) <= 1: error("The last target can not be removed.") return tcoll.delete({'name': name}) # if the target is the default one, # we need chose one target as default tcoll = db.get_collection("targets") dts = tcoll.find({'default': True}) if len(dts)==0: ts = tcoll.find() if ts and len(ts) > 0: tcoll.update({'default': True}, {'name': ts[0]['name']}) else: ts = tcoll.find() if ts and len(ts) > 0: tcoll.update({'default': True}, {'name': ts[0]['name']})
def remove_user(self, name=None, email=None, token=None): with offtheshelf.openDB(self.dbname) as db: coll = db.get_collection("users") cond = {} if token: cond['token'] = token if email: cond['email'] = email if name: cond['name'] = name coll.delete(cond) # if the user is the default one, # we need chose a user as default coll = db.get_collection("users") dus = coll.find({'default': True}) if len(dus)==0: ts = coll.find() if ts and len(ts) > 0: coll.update({'default': True}, {'name': ts[0]['name']})
def get_users(self): with offtheshelf.openDB(self.dbname) as db: tcoll = db.get_collection("users") return tcoll.find()