Skip to content

Commit 00ec445

Browse files
committed
enable login
1 parent febb1ca commit 00ec445

File tree

9 files changed

+92
-16
lines changed

9 files changed

+92
-16
lines changed

geoengine/auth.py

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
Module for encapsulating Geo Engine authentication
33
'''
44

5-
from typing import ClassVar, Dict, Optional
5+
from typing import ClassVar, Dict, Optional, Tuple
66
from uuid import UUID
77

8+
import os
89
import requests as req
910
from requests.auth import AuthBase
1011

@@ -35,14 +36,25 @@ class Session:
3536

3637
session: ClassVar[req.Session] = None
3738

38-
def __init__(self, server_url: str) -> None:
39+
def __init__(self, server_url: str, credentials: Tuple[str, str] = None) -> None:
3940
'''
4041
Initialize communication between this library and a Geo Engine instance
42+
43+
if credentials are provided, the session will be authenticated
44+
45+
optional arguments: (email, password) as tuple
46+
optional environment variables: GEOENGINE_EMAIL, GEOENGINE_PASSWORD
4147
'''
4248

43-
# TODO: username and password for Pro version
49+
session = None
4450

45-
session = req.post(f'{server_url}/anonymous').json()
51+
if credentials is not None:
52+
session = req.post(f'{server_url}/login', json={"email": credentials[0], "password": credentials[1]}).json()
53+
elif "GEOENGINE_EMAIL" in os.environ and "GEOENGINE_PASSWORD" in os.environ:
54+
session = req.post(f'{server_url}/login',
55+
json={"email": os.environ.get("GEOENGINE_EMAIL"), "password": os.environ.get("GEOENGINE_PASSWORD")}).json()
56+
else:
57+
session = req.post(f'{server_url}/anonymous').json()
4658

4759
if 'error' in session:
4860
raise GeoEngineException(session)
@@ -87,6 +99,13 @@ def requests_bearer_auth(self) -> BearerAuth:
8799

88100
return BearerAuth(self.__id)
89101

102+
def logout(self):
103+
'''
104+
Logout the current session
105+
'''
106+
107+
req.post(f'{self.server_url}/logout', headers=self.auth_header)
108+
90109

91110
def get_session() -> Session:
92111
'''
@@ -101,19 +120,25 @@ def get_session() -> Session:
101120
return Session.session
102121

103122

104-
def initialize(server_url: str) -> None:
123+
def initialize(server_url: str, credentials: Tuple[str, str] = None) -> None:
105124
'''
106125
Initialize communication between this library and a Geo Engine instance
126+
127+
if credentials are provided, the session will be authenticated
128+
129+
optional arugments: (email, password) as tuple
130+
optional environment variables: GEOENGINE_EMAIL, GEOENGINE_PASSWORD
107131
'''
108132

109-
Session.session = Session(server_url)
133+
Session.session = Session(server_url, credentials)
110134

111135

112-
def reset() -> None:
136+
def reset(logout: bool = True) -> None:
113137
'''
114138
Resets the current session
115139
'''
116140

117-
# TODO: active logout for Pro version
141+
if Session.session is not None and logout:
142+
Session.session.logout()
118143

119144
Session.session = None

tests/test_auth.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from datetime import datetime
44

55
import unittest
6+
import json
7+
import os
68
import requests_mock
79

810
import geoengine as ge
@@ -13,7 +15,9 @@ class AuthTests(unittest.TestCase):
1315
'''Tests runner regarding Geo Engine authentication'''
1416

1517
def setUp(self) -> None:
16-
ge.reset()
18+
assert "GEOENGINE_EMAIL" not in os.environ and "GEOENGINE_PASSWORD" not in os.environ, \
19+
"Please unset GEOENGINE_EMAIL and GEOENGINE_PASSWORD"
20+
ge.reset(False)
1721

1822
def test_uninitialized(self):
1923
with self.assertRaises(ge.UninitializedException) as exception:
@@ -45,6 +49,53 @@ def test_initialize(self):
4549
self.assertEqual(type(ge.get_session()),
4650
ge.Session)
4751

52+
def test_initialize_tuple(self):
53+
with requests_mock.Mocker() as m:
54+
m.post('http://mock-instance/login',
55+
additional_matcher=lambda request: request.text == json.dumps({"email": "[email protected]", "password": "secret123"}),
56+
json={
57+
"id": "e327d9c3-a4f3-4bd7-a5e1-30b26cae8064",
58+
"user": {
59+
"id": "328ca8d1-15d7-4f59-a989-5d5d72c98744",
60+
},
61+
"created": "2021-06-08T15:22:22.605891994Z",
62+
"validUntil": "2021-06-08T16:22:22.605892183Z",
63+
"project": None,
64+
"view": None
65+
})
66+
67+
ge.initialize("http://mock-instance", ("[email protected]", "secret123"))
68+
69+
self.assertEqual(type(ge.get_session()),
70+
ge.Session)
71+
72+
def test_initialize_env(self):
73+
with requests_mock.Mocker() as m:
74+
m.post('http://mock-instance/login',
75+
additional_matcher=lambda request: request.text == json.dumps({"email": "[email protected]", "password": "secret123"}),
76+
json={
77+
"id": "e327d9c3-a4f3-4bd7-a5e1-30b26cae8064",
78+
"user": {
79+
"id": "328ca8d1-15d7-4f59-a989-5d5d72c98744",
80+
},
81+
"created": "2021-06-08T15:22:22.605891994Z",
82+
"validUntil": "2021-06-08T16:22:22.605892183Z",
83+
"project": None,
84+
"view": None
85+
})
86+
87+
os.environ["GEOENGINE_EMAIL"] = "[email protected]"
88+
os.environ["GEOENGINE_PASSWORD"] = "secret123"
89+
90+
try:
91+
ge.initialize("http://mock-instance")
92+
finally:
93+
del os.environ["GEOENGINE_EMAIL"]
94+
del os.environ["GEOENGINE_PASSWORD"]
95+
96+
self.assertEqual(type(ge.get_session()),
97+
ge.Session)
98+
4899

49100
if __name__ == '__main__':
50101
unittest.main()

tests/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class WmsTests(unittest.TestCase):
1414
'''Test runner for the plotting functionality'''
1515

1616
def setUp(self) -> None:
17-
ge.reset()
17+
ge.reset(False)
1818

1919
def test_ndvi_histogram(self):
2020
with requests_mock.Mocker() as m:

tests/test_provenance.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class ProvenanceTests(unittest.TestCase):
1111
'''Test runner for provenance tests'''
1212

1313
def setUp(self) -> None:
14-
ge.reset()
14+
ge.reset(False)
1515

1616
def test_provenance_call(self):
1717
with requests_mock.Mocker() as m:

tests/test_upload.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class UploadTests(unittest.TestCase):
1414
'''Test runner regarding upload functionality'''
1515

1616
def setUp(self) -> None:
17-
ge.reset()
17+
ge.reset(False)
1818

1919
def test_upload(self):
2020
with requests_mock.Mocker() as m:

tests/test_wcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class WcsTests(unittest.TestCase):
1414
'''WCS test runner'''
1515

1616
def setUp(self) -> None:
17-
ge.reset()
17+
ge.reset(False)
1818

1919
def test_ndvi(self):
2020
with requests_mock.Mocker() as m, open("tests/responses/ndvi.tiff", "rb") as ndvi_tiff:

tests/test_wfs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WfsTests(unittest.TestCase):
1818
'''WFS test runner'''
1919

2020
def setUp(self) -> None:
21-
ge.reset()
21+
ge.reset(False)
2222

2323
def test_geopandas(self):
2424
with requests_mock.Mocker() as m:

tests/test_wms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class WmsTests(unittest.TestCase):
1818
'''WMS test runner'''
1919

2020
def setUp(self) -> None:
21-
ge.reset()
21+
ge.reset(False)
2222

2323
@responses.activate
2424
@ImageTesting(['wms'], tolerance=0)

tests/test_workflow_storage.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class WorkflowStorageTests(unittest.TestCase):
1515
'''Test methods for storing workflows as datasets'''
1616

1717
def setUp(self) -> None:
18-
ge.reset()
18+
ge.reset(False)
1919

2020
def test_storing_workflow(self):
2121
with requests_mock.Mocker() as m:

0 commit comments

Comments
 (0)