From 6b09dbabd12e7baeb01f585af852213a1c06b743 Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Mon, 18 Sep 2023 11:36:59 +0200
Subject: [PATCH] fix: lintn test: add test for annotation redirect

---
 .github/workflows/ci.yml                          | 15 +++++++--------
 backend/app/sane_url.py                           | 11 +++++++++--
 backend/pytest.ini                                |  4 ++++
 backend/test_app/test_sane_url.py                 | 13 +++++++++++++
 .../userAnnotations/tools/service.ts              |  4 ++--
 5 files changed, 35 insertions(+), 12 deletions(-)
 create mode 100644 backend/pytest.ini
 create mode 100644 backend/test_app/test_sane_url.py

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 1b17f4974..80b4dfbfc 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -58,16 +58,15 @@ jobs:
   backend:
     if: always()
     runs-on: ubuntu-latest
-
-    env:
-      NODE_ENV: test
-
     steps:
     - uses: actions/checkout@v3
-    - name: Use Node.js 16.x
-      uses: actions/setup-node@v1
+    - uses: actions/setup-python@v4
       with:
-        node-version: 16.x
+        python-version: '3.10'
     - run: |
-        echo "busybody"
+        cd backend
+        pip install -r requirements.txt
+        pip install pytest
+        pytest
+
   
diff --git a/backend/app/sane_url.py b/backend/app/sane_url.py
index dc4a4e347..29f14078e 100644
--- a/backend/app/sane_url.py
+++ b/backend/app/sane_url.py
@@ -27,6 +27,7 @@ vip_routes = [
 
 class SaneUrlDPStore(DataproxyStore):
     class AlreadyExists(Exception): ...
+    class NotWritable(IOError): ...
 
     @staticmethod
     def GetTimeMs() -> int:
@@ -36,8 +37,12 @@ class SaneUrlDPStore(DataproxyStore):
     def TransformKeyToObjName(key: str):
         return f"saneUrl/{key}.json"
     
+    writable = False
+
     def __init__(self, expiry_s=3 * 24 * 60 * 60):
-                
+        if not (SXPLR_EBRAINS_IAM_SA_CLIENT_ID and SXPLR_EBRAINS_IAM_SA_CLIENT_SECRET):
+            super().__init__(None, SXPLR_BUCKET_NAME)
+            return
         resp = requests.get(f"{EBRAINS_IAM_DISCOVERY_URL}/.well-known/openid-configuration")
         resp.raise_for_status()
         resp_json = resp.json()
@@ -54,6 +59,7 @@ class SaneUrlDPStore(DataproxyStore):
         self._refresh_token()
 
         super().__init__(self.token, SXPLR_BUCKET_NAME)
+        self.writable = True
     
     def _refresh_token(self):
         token_dict = self.session.fetch_token(self._token_endpoint, grant_type="client_credentials")
@@ -94,7 +100,8 @@ class SaneUrlDPStore(DataproxyStore):
             raise SaneUrlDPStore.GenericException(str(e)) from e
 
     def set(self, key: str, value: Union[str, Dict], request: Optional[Request]=None):
-        
+        if not self.writable:
+            raise SaneUrlDPStore.NotWritable
         object_name = SaneUrlDPStore.TransformKeyToObjName(key)
         try:
             super().get(object_name)
diff --git a/backend/pytest.ini b/backend/pytest.ini
new file mode 100644
index 000000000..0d140f30b
--- /dev/null
+++ b/backend/pytest.ini
@@ -0,0 +1,4 @@
+[pytest]
+pythonpath = .
+testpaths = 
+    test_app
\ No newline at end of file
diff --git a/backend/test_app/test_sane_url.py b/backend/test_app/test_sane_url.py
new file mode 100644
index 000000000..49ab5ec49
--- /dev/null
+++ b/backend/test_app/test_sane_url.py
@@ -0,0 +1,13 @@
+from app.app import app
+from fastapi.testclient import TestClient
+
+client = TestClient(app)
+
+def test_annotation_redirect():
+    resp = client.get("/go/stnr", headers={
+        "Accept": "text/html"
+    }, follow_redirects=False)
+    loc = resp.headers.get("Location")
+    assert loc, "Expected location header to be present, but was not"
+    assert "x-user-anntn:stnr" in loc, f"Expected the string 'x-user-anntn:stnr' in {loc!r}, but was not"
+
diff --git a/src/atlasComponents/userAnnotations/tools/service.ts b/src/atlasComponents/userAnnotations/tools/service.ts
index f1e2e2689..bb0b092d7 100644
--- a/src/atlasComponents/userAnnotations/tools/service.ts
+++ b/src/atlasComponents/userAnnotations/tools/service.ts
@@ -482,10 +482,10 @@ export class ModularUserAnnotationToolService implements OnDestroy{
             return this.annotationLayer
           })
           ).pipe(
-            map(annotationLayer => ({viewerMode, voxelSize, annotationLayer}))
+            map(annotationLayer => ({viewerMode, annotationLayer}))
           )
         )
-      ).subscribe(({viewerMode, voxelSize, annotationLayer}) => {
+      ).subscribe(({viewerMode, annotationLayer}) => {
         this.currMode = viewerMode
         
         /**
-- 
GitLab