def setUp(self): self.tf = Tempfiles() self.troot = self.tf.mkdir("siphandler") self.revdir = os.path.join(self.troot, "review") os.mkdir(self.revdir) self.workdir = os.path.join(self.troot, "working") os.mkdir(self.workdir) self.stagedir = os.path.join(self.troot, "staging") # os.mkdir(self.stagedir) self.mdserv = os.path.join(self.troot, "mdserv") os.mkdir(self.mdserv) self.store = os.path.join(self.troot, "store") os.mkdir(self.store) self.statusdir = os.path.join(self.troot, "status") os.mkdir(self.statusdir) shutil.copytree(self.sipdata, os.path.join(self.revdir, "1491")) with open(os.path.join(datadir, "bagger_conf.yml")) as fd: baggercfg = yaml.load(fd) self.config = { "working_dir": self.workdir, "store_dir": self.store, "id_registry_dir": self.workdir, "sip_type": { "midas": { "common": { "review_dir": self.revdir, "id_minter": { "shoulder_for_edi": "edi0" }, }, "mdserv": { "working_dir": self.mdserv }, "preserv": { "bagparent_dir": "_preserv", "staging_dir": self.stagedir, "bagger": baggercfg, "status_manager": { "cachedir": self.statusdir }, } } } } try: self.svc = wsgi.app(self.config) except Exception, e: self.tearDown() raise
cfg = srvc.get(uwsgi.opt.get('oar-config_appname', 'pdr-publish')) elif config.service: config.service.wait_until_up(int(os.environ.get('OAR_CONFIG_TIMEOUT', 10)), True, sys.stderr) cfg = config.service.get(os.environ.get('OAR_CONFIG_APP', 'pdr-publish')) elif is_in_test_mode(): cfg = {} else: raise ConfigurationException("preserver: nist-oar configuration not "+ "provided") cfg = update_if_test_mode(cfg) logcfg = cfg.get('sip_type',{}).get('midas',{}).get('preserv',{}) if 'logfile' not in logcfg: logcfg['logfile'] = 'preservation.log' config.configure_log(config=logcfg, addstderr=True) if cfg.get('test_mode'): logging.info("Starting server in test mode with work_dir=%s, midas_dir=%s, "+ "store=%s", cfg.get('working_dir'), cfg.get('test_data_dir'), cfg.get('store_dir')) mdwd = cfg.get('sip_type',{}).get('midas',{}).get('mdserv',{}) \ .get('working_dir') if mdwd: if os.path.exists(mdwd): clean_working_dir(mdcfg.get('working_dir')) else: os.mkdir(mdwd) application = wsgi.app(cfg)
def test_auth(self): # test rejection when auth key provide but wsgi configured to take one req = { 'PATH_INFO': '/midas/' + self.midasid, 'REQUEST_METHOD': 'GET', 'QUERY_STRING': 'goob=able&auth=9e73' } body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertEqual(body, []) # now configure the service to require a key cfg = deepcopy(self.config) cfg['auth_key'] = '9e73' self.svc = wsgi.app(cfg) # test successful acceptance of key self.resp = [] body = self.svc(req, self.start) self.assertGreater(len(self.resp), 0) self.assertIn("404", self.resp[0]) self.assertGreater(len(body), 0) data = json.loads(body[0]) self.assertEqual(data['id'], '3A1EE2F169DD3B8CE0531A570681DB5D1491') self.assertEqual(data['state'], "ready") # test that acceptance of last key provided req['QUERY_STRING'] = 'goob=able&auth=gurn&auth=9e73' self.resp = [] body = self.svc(req, self.start) self.assertGreater(len(self.resp), 0) self.assertIn("404", self.resp[0]) self.assertGreater(len(body), 0) data = json.loads(body[0]) self.assertEqual(data['id'], '3A1EE2F169DD3B8CE0531A570681DB5D1491') self.assertEqual(data['state'], "ready") req['QUERY_STRING'] = 'goob=able&auth=9e73&auth=gurn' self.resp = [] body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertEqual(body, []) # test single rejections req['QUERY_STRING'] = 'goob=able&auth=gurn' self.resp = [] body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertEqual(body, []) # test lack of auth key del req['QUERY_STRING'] self.resp = [] body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertEqual(body, []) # test header access key cfg['auth_method'] = 'header' self.svc = wsgi.app(cfg) self.resp = [] body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertIn("WWW-Authenticate: Bearer", self.resp) self.assertEqual(body, []) self.resp = [] req['HTTP_AUTHORIZATION'] = 'Bearer 9e73' body = self.svc(req, self.start) self.assertGreater(len(self.resp), 0) self.assertIn("404", self.resp[0]) self.assertGreater(len(body), 0) data = json.loads(body[0]) self.assertEqual(data['id'], '3A1EE2F169DD3B8CE0531A570681DB5D1491') self.assertEqual(data['state'], "ready") self.resp = [] req['HTTP_AUTHORIZATION'] = 'Token 9e73' body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertIn("WWW-Authenticate: Bearer", self.resp) self.assertEqual(body, []) self.resp = [] req['HTTP_AUTHORIZATION'] = 'Bearer' body = self.svc(req, self.start) self.assertIn("401", self.resp[0]) self.assertIn("WWW-Authenticate: Bearer", self.resp) self.assertEqual(body, [])