Skip to content

Commit

Permalink
Merge pull request #22 from Alqor-UG/21-verify-the-logindict
Browse files Browse the repository at this point in the history
Verify the logindict
  • Loading branch information
fretchen authored Feb 11, 2024
2 parents d17a0b0 + 6a18d14 commit ffe528b
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/qlued/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
from django.db import models
from django.core.exceptions import ValidationError

from sqooler.schemes import (
MongodbLoginInformation,
DropboxLoginInformation,
LocalLoginInformation,
)


class Token(models.Model):
"""
Expand Down Expand Up @@ -84,3 +90,15 @@ def clean(self):
"name": "The name of the storage provider cannot contain spaces or underscores."
}
)
# make sure that the login dict is valid
if self.storage_type == "dropbox":

DropboxLoginInformation(**self.login)
elif self.storage_type == "mongodb":
MongodbLoginInformation(**self.login)
elif self.storage_type == "local":
LocalLoginInformation(**self.login)
else:
raise ValidationError(
{"storage_type": f"Value '{self.storage_type}' is not a valid choice."}
)
12 changes: 12 additions & 0 deletions tests/test_local_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,18 @@ def test_localdb_object(self):
login_info = LocalLoginInformation(**poor_login_dict)
LocalProvider(login_info, mongodb_entry.name)

# create the storage entry in the models
local_entry = StorageProviderDb.objects.create(
storage_type="local",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=poor_login_dict,
is_active=True,
)
with self.assertRaises(ValidationError):
local_entry.full_clean()

def test_not_active(self):
"""
Test that we cannot work with the provider if it is not active.
Expand Down
6 changes: 3 additions & 3 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def test_mongo_storage_db_creation(self):
mongodb_password = config("MONGODB_PASSWORD")
mongodb_database_url = config("MONGODB_DATABASE_URL")
login_dict = {
"username": mongodb_username,
"password": mongodb_password,
"database_url": mongodb_database_url,
"mongodb_username": mongodb_username,
"mongodb_password": mongodb_password,
"mongodb_database_url": mongodb_database_url,
}

# create the storage entry in the models
Expand Down
103 changes: 103 additions & 0 deletions tests/test_storage_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@

from django.contrib.auth import get_user_model
from django.test import TestCase
from django.db import IntegrityError

from sqooler.storage_providers.local import LocalProviderExtended as LocalProvider
from sqooler.schemes import LocalLoginInformation, BackendConfigSchemaIn

from pydantic import ValidationError

from qlued.models import StorageProviderDb

from qlued.storage_providers import (
Expand All @@ -33,6 +37,7 @@ def setUp(self):
user = User.objects.create(username=self.username)
user.set_password(self.password)
user.save()
self.user = user

# add the first storage provider
base_path = "storage-3"
Expand Down Expand Up @@ -140,3 +145,101 @@ def test_get_storage_provider_from_entry(self):

full_backend_name = "local2_singlequdit_simulator"
storage_provider = get_storage_provider(full_backend_name)

def test_add_local_provider(self):
"""
Test that we can add a local provider
"""
# create the storage entry in the models with a poor login dict
poor_login_dict = {
"app_key_t": "test",
"app_secret": "test",
"refresh_token": "test",
}
local_entry = StorageProviderDb.objects.create(
storage_type="local",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=poor_login_dict,
is_active=True,
)
with self.assertRaises(ValidationError):
local_entry.full_clean()

local_entry.delete()

# create the storage entry in the models
login_dict = {"base_path": "test"}
local_entry = StorageProviderDb.objects.create(
storage_type="local",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=login_dict,
is_active=True,
)

local_entry.full_clean()

# make sure that the name is unique
with self.assertRaises(IntegrityError):
StorageProviderDb.objects.create(
storage_type="local",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=login_dict,
is_active=True,
)

def test_add_mongodb_provider(self):
"""
Test that we can add a MongoDB provider
"""
# create the storage entry in the models with a poor login dict
poor_login_dict = {
"app_key_t": "test",
"app_secret": "test",
"refresh_token": "test",
}
local_entry = StorageProviderDb.objects.create(
storage_type="mongodb",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=poor_login_dict,
is_active=True,
)
with self.assertRaises(ValidationError):
local_entry.full_clean()

local_entry.delete()

# create the storage entry in the models
login_dict = {
"mongodb_database_url": "test",
"mongodb_username": "test",
"mongodb_password": "test",
}
local_entry = StorageProviderDb.objects.create(
storage_type="mongodb",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=login_dict,
is_active=True,
)

local_entry.full_clean()

# make sure that the name is unique
with self.assertRaises(IntegrityError):
StorageProviderDb.objects.create(
storage_type="mongodb",
name="localtest342",
owner=self.user,
description="Local storage provider for tests",
login=login_dict,
is_active=True,
)

0 comments on commit ffe528b

Please sign in to comment.