Skip to content

Commit

Permalink
test: add tests for credentials feature (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
CGuichard committed Dec 21, 2021
1 parent ce52ea2 commit 628a778
Show file tree
Hide file tree
Showing 5 changed files with 533 additions and 3 deletions.
12 changes: 11 additions & 1 deletion tests/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@
from eodag.api.product.metadata_mapping import format_metadata
from eodag.api.search_result import SearchResult
from eodag.cli import download, eodag, list_pt, search_crunch
from eodag.config import load_default_config, merge_configs
from eodag.config import (
load_default_config,
merge_configs,
get_provider_credentials,
load_providers_credentials,
override_credentials_from_mapping,
save_providers_credentials,
set_provider_credentials_key,
)
from eodag.plugins.authentication.base import Authentication
from eodag.plugins.crunch.filter_date import FilterDate
from eodag.plugins.crunch.filter_latest_tpl_name import FilterLatestByName
Expand All @@ -54,6 +62,7 @@
ProgressCallback,
uri_to_path,
)
from eodag.utils.cli import ask_confirmation
from eodag.utils.exceptions import (
AddressNotFound,
AuthenticationError,
Expand All @@ -65,5 +74,6 @@
UnsupportedProvider,
ValidationError,
)
from eodag.utils.security import Crypto, CryptoKey
from eodag.utils.stac_reader import fetch_stac_items
from tests import TESTS_DOWNLOAD_PATH, TEST_RESOURCES_PATH
281 changes: 280 additions & 1 deletion tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import os
import random
import tempfile
import unittest
from contextlib import contextmanager

Expand Down Expand Up @@ -329,7 +330,7 @@ def test_eodag_search_with_cruncher(self, dag):
api_obj.crunch.assert_called_with(
search_results,
search_criteria=criteria,
**{cruncher: {"minimum_overlap": "10"}}
**{cruncher: {"minimum_overlap": "10"}},
)

@mock.patch("eodag.cli.EODataAccessGateway", autospec=True)
Expand Down Expand Up @@ -496,3 +497,281 @@ def test_eodag_download_wrongcredentials(self, download):
self.assertEqual(result.exit_code, 1)
self.assertIsInstance(result.exception, AuthenticationError)
self.assertEqual(download.call_count, 1)

def test_eodag_credentials_provider_not_found(self):
with tempfile.NamedTemporaryFile() as f:
result = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "notexists"]
)
self.assertEqual(result.exit_code, -1)

@mock.patch("getpass.getpass")
def test_eodag_credentials_set(self, getpass_mock):
getpass_mock.return_value = "mocked_value"
with tempfile.NamedTemporaryFile() as f:
result = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo"]
)
self.assertEqual(result.exit_code, 0)

@mock.patch("builtins.input")
def test_eodag_credentials_already_saved_not_override(self, input_mock):
input_mock.return_value = "n"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
"apikey=abcd",
],
)
insert_creds2 = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(insert_creds2.exit_code, 1)

@mock.patch("getpass.getpass")
@mock.patch("builtins.input")
def test_eodag_credentials_already_saved_override(self, input_mock, getpass_mock):
getpass_mock.return_value = "mocked_value"
input_mock.return_value = "y"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
"apikey=abcd",
],
)
insert_creds2 = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(insert_creds2.exit_code, 0)

def test_eodag_credentials_set_from_cli(self):
with tempfile.NamedTemporaryFile() as f:
result = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"peps",
"-y",
"-s",
"username=user1",
"-s",
"password=secret",
],
)
self.assertEqual(result.exit_code, 0)

def test_eodag_credentials_set_from_cli_missing_field(self):
with tempfile.NamedTemporaryFile() as f:
result = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"peps",
"-y",
"-s",
"username=user1",
],
)
self.assertEqual(result.exit_code, -1)

def test_eodag_credentials_list(self):
result = self.runner.invoke(eodag, ["credentials", "peps", "-l"])
self.assertTrue("username" in result.stdout)
self.assertTrue("password" in result.stdout)
self.assertEqual(result.exit_code, 0)

def test_eodag_credentials_exists(self):
with tempfile.NamedTemporaryFile() as f:
check_not_exists = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-e"]
)
insert_creds = self.runner.invoke(
eodag,
["credentials", "--creds", f.name, "sobloo", "-y", "-s", "apikey=abcd"],
)
check_exists = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-e"]
)
self.assertEqual(check_not_exists.exit_code, -1)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(check_exists.exit_code, 0)

def test_eodag_credentials_read_no_credentials_found(self):
with tempfile.NamedTemporaryFile() as f:
result = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-ry"]
)
self.assertEqual(result.exit_code, -1)

def test_eodag_credentials_read_autoconfirm(self):
apikey = "abcd"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
f"apikey={apikey}",
],
)
read_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-ry"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(read_creds.exit_code, 0)
self.assertTrue(apikey in read_creds.stdout)

@mock.patch("builtins.input")
def test_eodag_credentials_read_confirm(self, input_mock):
input_mock.return_value = "y"
apikey = "abcd"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
f"apikey={apikey}",
],
)
read_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-r"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(read_creds.exit_code, 0)
self.assertTrue(apikey in read_creds.stdout)

@mock.patch("builtins.input")
def test_eodag_credentials_read_not_confirm(self, input_mock):
input_mock.return_value = "n"
apikey = "abcd"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
f"apikey={apikey}",
],
)
read_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-r"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(read_creds.exit_code, 1)
self.assertTrue(apikey not in read_creds.stdout)

def test_eodag_credentials_delete_no_credentials_found(self):
with tempfile.NamedTemporaryFile() as f:
delete_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-d"]
)
self.assertEqual(delete_creds.exit_code, -1)

def test_eodag_credentials_delete_autoconfirm(self):
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
"apikey=abcd",
],
)
delete_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-dy"]
)
check_exists = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-e"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(delete_creds.exit_code, 0)
self.assertEqual(check_exists.exit_code, -1)

@mock.patch("builtins.input")
def test_eodag_credentials_delete_confirm(self, input_mock):
input_mock.return_value = "y"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
"apikey=abcd",
],
)
delete_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-d"]
)
check_exists = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-e"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(delete_creds.exit_code, 0)
self.assertEqual(check_exists.exit_code, -1)

@mock.patch("builtins.input")
def test_eodag_credentials_delete_not_confirm(self, input_mock):
input_mock.return_value = "n"
with tempfile.NamedTemporaryFile() as f:
insert_creds = self.runner.invoke(
eodag,
[
"credentials",
"--creds",
f.name,
"sobloo",
"-y",
"-s",
"apikey=abcd",
],
)
delete_creds = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-d"]
)
check_exists = self.runner.invoke(
eodag, ["credentials", "--creds", f.name, "sobloo", "-e"]
)
self.assertEqual(insert_creds.exit_code, 0)
self.assertEqual(delete_creds.exit_code, 1)
self.assertEqual(check_exists.exit_code, 0)
Loading

0 comments on commit 628a778

Please sign in to comment.