def test_configSet_argument_optmap_of_invalid_type_should_raise_TypeError(self): """ Test configSet(self, optMap=dict()) """ sfdb = SpiderFootDb(self.default_options, False) invalid_types = [None, "", list()] for invalid_type in invalid_types: with self.subTest(invalid_type=invalid_type): with self.assertRaises(TypeError): sfdb.configSet(invalid_type)
def test_configSet_should_set_config_opts(self): """ Test configSet(self, optMap=dict()) """ sfdb = SpiderFootDb(self.default_options, False) opts = dict() opts['example'] = 'example non-default config opt' sfdb.configSet(opts) config = sfdb.configGet() self.assertIsInstance(config, dict) self.assertIn('example', config) self.assertEqual('TBD', 'TBD')
def test_configClear_should_clear_config(self): """ Test configClear(self) """ sfdb = SpiderFootDb(self.default_options, False) opts = dict() opts['example'] = 'example non-default config opt' sfdb.configSet(opts) config = sfdb.configGet() self.assertIsInstance(config, dict) self.assertIn('example', config) sfdb.configClear() config = sfdb.configGet() self.assertIsInstance(config, dict) self.assertNotIn('example', config)
def savesettingsraw(self, allopts, token): """Save settings, also used to completely reset them to default Args: allopts: TBD token: CSRF token Returns: str: save success as JSON """ cherrypy.response.headers['Content-Type'] = "application/json; charset=utf-8" if str(token) != str(self.token): return json.dumps(["ERROR", "Invalid token (%s)." % self.token]).encode('utf-8') # Reset config to default if allopts == "RESET": if self.reset_settings(): return json.dumps(["SUCCESS", ""]).encode('utf-8') return json.dumps(["ERROR", "Failed to reset settings"]).encode('utf-8') # Save settings try: dbh = SpiderFootDb(self.config) useropts = json.loads(allopts) cleanopts = dict() for opt in list(useropts.keys()): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return json.dumps(["ERROR", "Processing one or more of your inputs failed: %s" % e]).encode('utf-8') return json.dumps(["SUCCESS", ""]).encode('utf-8')
def savesettings(self, allopts, token, configFile=None): """Save settings, also used to completely reset them to default Args: allopts: TBD token: TBD configFile: TBD """ if str(token) != str(self.token): return self.error("Invalid token (%s)" % self.token) if configFile: # configFile seems to get set even if a file isn't uploaded if configFile.file: contents = configFile.file.read() if type(contents) == bytes: contents = contents.decode('utf-8') try: tmp = dict() for line in contents.split("\n"): if "=" not in line: continue opt_array = line.strip().split("=") if len(opt_array) == 1: opt_array[1] = "" tmp[opt_array[0]] = '='.join(opt_array[1:]) allopts = json.dumps(tmp).encode('utf-8') except BaseException as e: return self.error("Failed to parse input file. Was it generated from SpiderFoot? (%s)" % e) # Reset config to default if allopts == "RESET": if self.reset_settings(): raise cherrypy.HTTPRedirect("/opts?updated=1") else: return self.error("Failed to reset settings") # Save settings try: dbh = SpiderFootDb(self.config) useropts = json.loads(allopts) cleanopts = dict() for opt in list(useropts.keys()): cleanopts[opt] = self.cleanUserInput([useropts[opt]])[0] currentopts = deepcopy(self.config) # Make a new config where the user options override # the current system config. sf = SpiderFoot(self.config) self.config = sf.configUnserialize(cleanopts, currentopts) dbh.configSet(sf.configSerialize(self.config)) except Exception as e: return self.error("Processing one or more of your inputs failed: %s" % e) raise cherrypy.HTTPRedirect("/opts?updated=1")