예제 #1
0
파일: server_test.py 프로젝트: rth/icubam
    def test_map(self):
        # No key
        route = home.MapByAPIHandler.ROUTE
        response = self.fetch(route, method="GET")
        self.assertEqual(response.code, 503)

        # Wrong key
        url = f'{route}?API_KEY=aaaaa'
        response = self.fetch(url, method="GET")
        self.assertEqual(response.code, 503)
        access_stats = store.ExternalClient(
            name='stats-key', access_type=store.AccessTypes.STATS)
        access_token_id, access_key = self.db.add_external_client(
            self.admin_id, access_stats)
        url = f'{route}?API_KEY={access_key.key}'
        response = self.fetch(url, method="GET")
        self.assertEqual(response.code, 503)

        # Good access type
        access_maps = store.ExternalClient(name='maps-key',
                                           access_type=store.AccessTypes.MAP)
        access_token_id, access_key = self.db.add_external_client(
            self.admin_id, access_maps)
        url = f'{route}?API_KEY={access_key.key}'
        response = self.fetch(url, method="GET")
        self.assertEqual(response.code, 200)

        # All access type
        access_all = store.ExternalClient(name='all-access',
                                          access_type=store.AccessTypes.ALL)
        access_token_id, access_key = self.db.add_external_client(
            self.admin_id, access_all)
        url = f'{route}?API_KEY={access_key.key}'
        response = self.fetch(url, method="GET")
        self.assertEqual(response.code, 200)
예제 #2
0
    def post(self):
        values = self.parse_from_body(store.ExternalClient)
        id_key = 'external_client_id'
        token_id = values.pop(id_key, '')
        try:
            if not token_id:
                token_id = self.db.add_external_client(
                    self.user.user_id, store.ExternalClient(**values))
            else:
                self.db.update_external_client(self.user.user_id, token_id,
                                               values)
        except Exception as e:
            logging.error(f'cannot save token {e}')
            values[id_key] = token_id
            return self.do_render(store.ExternalClient(**values), error=True)

        return self.redirect(ListTokensHandler.ROUTE)
예제 #3
0
    def post(self):
        values = self.parse_from_body(store.ExternalClient)
        token_id, regions = self.prepare_for_save(values)
        try:
            save_fn = self.create_token if not token_id else self.update_token
            save_fn(token_id, values, regions)
        except Exception as e:
            logging.error(f'cannot save token {e}')
            values[self.ID_KEY] = token_id
            return self.do_render(store.ExternalClient(**values),
                                  regions,
                                  error=True)

        return self.redirect(ListTokensHandler.ROUTE)
예제 #4
0
def main(argv):
  fields = ['name', 'telephone', 'email']
  values = {k: FLAGS[k].value for k in fields if FLAGS[k].value is not None}
  cfg = config.Config(FLAGS.config, mode=FLAGS.mode, env_path=FLAGS.dotenv_path)
  db_factory = store.create_store_factory_for_sqlite_db(cfg)
  db = db_factory.create()
  users = db.get_admins()
  if users:
    admin_id = users[0].user_id
  else:
    admin_id = db.add_default_admin()

  user_query = store.ExternalClient(**values)
  user = db.get_external_client_by_email(user_query.email)
  if user is None:
    logging.info("New access token: {}".format(
      db.add_external_client(admin_id, user_query)))
  else:
    c_id = user.external_client_id
    db.update_external_client(admin_id, c_id, values)
    logging.info(f"Updated client {c_id} with values: {values}")
예제 #5
0
 def do_render(self,
               user: Optional[store.ExternalClient],
               regions: Optional[List[int]],
               error=False):
     user = user if user is not None else store.ExternalClient()
     if user.is_active is None:
         user.is_active = True
     if user.access_type is None:
         user.access_type = store.AccessTypes.ALL
     elif isinstance(user.access_type, str):
         user.access_type = store.AccessTypes.__members__[user.access_type]
     date = ""
     if user.expiration_date is not None:
         date = user.expiration_date.strftime(self.TIME_FORMAT)
     options = self.db.get_regions()
     access_types = list(store.AccessTypes.__members__.keys())
     return self.render("token.html",
                        user=user,
                        options=options,
                        regions=regions,
                        error=error,
                        access_types=access_types,
                        date=date,
                        list_route=ListTokensHandler.ROUTE)
예제 #6
0
    def test_db_all_bedcounts(self):
        route = "/db/all_bedcounts?format=csv"
        access_all = store.ExternalClient(name='all-access',
                                          access_type=store.AccessTypes.ALL)
        access_token_id, access_key = self.db.add_external_client(
            self.admin_id, access_all)
        route = f'{route}&API_KEY={access_key.key}'

        def check_response_csv(self, response):
            """Check the the response CSV is well formatted"""
            self.assertEqual(response.code, 200)
            csv = StringIO(response.body.decode('utf-8'))
            df = pd.read_csv(csv)
            self.assertIn('icu_name', df.columns)
            self.assertGreater(df.shape[0], 0)

        # Check without preprocessing
        response = self.fetch(route, method="GET")
        self.assertEqual(response.code, 200)
        check_response_csv(self, response)

        # CSV with preprocessing
        response = self.fetch(f'{route}&preprocess=true', method="GET")
        check_response_csv(self, response)
예제 #7
0
 def create_token(self, token_id, values, regions):
     client_id, _ = self.db.add_external_client(
         self.current_user.user_id, store.ExternalClient(**values))
     for rid in regions:
         self.db.assign_external_client_to_region(self.current_user.user_id,
                                                  client_id, rid)
예제 #8
0
 def do_render(self, user: Optional[store.User], error=False):
     user = user if user is not None else store.ExternalClient()
     return self.render("token.html",
                        user=user,
                        error=error,
                        list_route=ListTokensHandler.ROUTE)