Skip to content

Commit

Permalink
start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
austinjhunt committed Nov 27, 2022
1 parent a7f9dbd commit 95fa0ab
Show file tree
Hide file tree
Showing 6 changed files with 275 additions and 6 deletions.
14 changes: 12 additions & 2 deletions GoogleSharePointMigrationAssistant/web/models.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from django.db import models
# https://docs.djangoproject.com/en/4.1/topics/i18n/translation/
from django.utils.translation import gettext_lazy as _
from django.contrib.auth.models import User

from django.db.models.signals import post_save
from django.dispatch import receiver

class Profile(models.Model):
class Meta:
Expand All @@ -11,6 +11,16 @@ class Meta:
user = models.OneToOneField(User, on_delete=models.CASCADE)
timezone = models.CharField(max_length=100, default="UTC")

# signals allowing auto-creation & update of
# profiles when users are created/updated
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()

class Migration(models.Model):
""" Store migration records for users """
Expand Down
3 changes: 0 additions & 3 deletions GoogleSharePointMigrationAssistant/web/tests.py

This file was deleted.

Empty file.
107 changes: 107 additions & 0 deletions GoogleSharePointMigrationAssistant/web/tests/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
TARGET_EXAMPLE = {
'type': 'sharepoint_folder',
'details': {
'site': {
"createdDateTime": "2022-03-03T16:00:01Z",
"description": "Lorem ipsum",
"id": "org.sharepoint.com,longid,longid",
"lastModifiedDateTime": "2022-11-21T19:44:05Z",
"name": "this-is-the-site-name",
"webUrl": "https://org.sharepoint.com/sites/this-is-the-site-name",
"displayName": "SharePoint Online Site Display Name",
"root": {},
"siteCollection": {
"hostname": "org.sharepoint.com"
}
},
'document_library': {
"createdDateTime": "2022-11-04T15:48:08Z",
"description": "My Test Doc Lib",
"id": "document-library-guid",
"lastModifiedDateTime": "2022-11-04T17:48:32Z",
"name": "My Test Doc Lib",
"webUrl": "https://org.sharepoint.com/sites/this-is-the-site-name/My%20Test%20Doc%20Lib",
"driveType": "documentLibrary",
"createdBy": {
"user": {
"email": "[email protected]",
"id": "user-guid-here",
"displayName": "full name of user"
}
},
"lastModifiedBy": {
"user": {
"email": "[email protected]",
"id": "user-guid-here",
"displayName": "full name of user"
}
},
"owner": {
"group": {
"email": "[email protected]",
"id": "group guid",
"displayName": "this-is-the-site-name Owners"
}
},
"quota": {}
},
'folder': {
"createdDateTime": "2022-11-04T15:51:26Z",
"eTag": "etag value",
"id": "folder-guid",
"lastModifiedDateTime": "2022-11-04T15:58:34Z",
"name": "My Folder 1",
"webUrl": "https://org.sharepoint.com/sites/this-is-the-site-name/My%20Test%20Doc%20Lib/My%20Folder%201",
"cTag": "ctag value",
"size": 20296,
"createdBy": {
"user": {
"email": "[email protected]",
"id": "user-guid-here",
"displayName": "full name of user"
}
},
"lastModifiedBy": {
"user": {
"email": "[email protected]",
"id": "user-guid-here",
"displayName": "full name of user"
}
},
"parentReference": {
"driveType": "documentLibrary",
"driveId": "document-library-guid",
"id": "longidofthisfolder",
"path": "/drives/document-library-guid/root:" # it's in the root of the doc lib
},
"fileSystemInfo": {
"createdDateTime": "2022-11-04T15:51:26Z",
"lastModifiedDateTime": "2022-11-04T15:58:34Z"
},
"folder": {
"childCount": 1
},
"shared": {
"scope": "users"
}
}
}
}

GOOGLE_FOLDER_SOURCE = {
'type': 'folder', # alternative is folder
'details': {
"kind": "drive#file",
"id": "scoobydoobydoo-folder",
"name": "really cool folder",
"mimeType": "application/vnd.google-apps.folder"
}
}
GOOGLE_SHARED_DRIVE_SOURCE = {
'type': 'shared_drive', # alternative is folder
'details': {
"id": "scoobydoobydoo-shared-drive",
"name": "super sick shared drive",
"kind": "drive#drive"
}
}
156 changes: 156 additions & 0 deletions GoogleSharePointMigrationAssistant/web/tests/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
from django.test import TestCase, override_settings
from ..models import Migration, User, Profile, AdministrationSettings
from .conf import *

@override_settings(
TEMPLATES=[
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
# DO NOT USE CUSTOM CONTEXT PROCESSORS FOR TESTING. THEY BREAK THE TESTS.
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
)
class BaseTest(TestCase):
def setUp(self):
config = AdministrationSettings(require_idp_login=False)
config.save()

def setup_view(self, view, request, *args, **kwargs):
"""
Mimic ``as_view()``, but returns view instance.
Use this function to get view instances on which you can run unit tests,
by testing specific methods.
"""
view.request = request
view.args = args
view.kwargs = kwargs
return view


class UserTestCase(BaseTest):
def setUp(self):
super(UserTestCase, self).setUp()
self.user = User.objects.create_user(
username='testuser',
email='[email protected]',
password='fakepass')

def test_profile(self):
try:
self.assertIsNotNone(Profile.objects.get(user=self.user))
except Profile.DoesNotExist:
self.assertTrue(False)

class LoginTestCase(UserTestCase):
def test_login_correct(self):
response = self.client.post(
'/login', data={'username': 'testuser', 'password': 'fakepass'})
self.assertEquals(302, response.status_code)

def test_login_incorrect(self):
response = self.client.post(
'/login', data={'username': 'testuser2', 'password': 'fakepass'})
self.assertNotEquals(302, response.status_code)


class LogoutTestCase(UserTestCase):
def test_logout(self):
response = self.client.get('/logout')
self.assertEquals(302, response.status_code)


class MigrationGoogleSharedDriveTestCase(BaseTest):
def setUp(self):
super(MigrationGoogleSharedDriveTestCase, self).setUp()
user = User.objects.create_user(
username='testuser',
email='[email protected]',
password='fakepass')
self.migration = Migration(
user=user,
num_files=20,
num_folders=2,
total_size='1.4GB',
local_temp_dir=user.username,
target=TARGET_EXAMPLE,
google_source=GOOGLE_SHARED_DRIVE_SOURCE
)


class MigrationGoogleFolderTestCase(BaseTest):
def setUp(self):
super(MigrationGoogleFolderTestCase, self).setUp()
user = User.objects.create_user(
username='testuser',
email='[email protected]',
password='fakepass')
self.migration = Migration(
user=user,
num_files=20, num_folders=2,
total_size='1.4GB',
google_source=GOOGLE_FOLDER_SOURCE,
target=TARGET_EXAMPLE
)
self.migration.save()

def test_source_type(self):
val = self.migration.source_type
self.assertEquals(val, 'folder')

def test_source_name(self):
val = self.migration.source_name
self.assertEquals(val, 'really cool folder')

def test_source_id(self):
val = self.migration.source_id
self.assertEquals(val, 'scoobydoobydoo-folder')

def test_target_type(self):
val = self.migration.target_type
self.assertEquals(val, 'sharepoint_folder')

def test_target_folder_id(self):
val = self.migration.target_folder_id
self.assertEquals(val, 'folder-guid')

def test_target_folder_name(self):
val = self.migration.target_folder_name
self.assertEquals(val, 'My Folder 1')

def test_target_document_library_id(self):
val = self.migration.target_document_library_id
self.assertEquals(val, 'document-library-guid')

def test_target_document_library_name(self):
val = self.migration.target_document_library_name
self.assertEquals(val, 'My Test Doc Lib')

def test_target_site_id(self):
val = self.migration.target_site_id
self.assertEquals(val, 'org.sharepoint.com,longid,longid')

def test_target_site_name(self):
val = self.migration.target_site_name
self.assertEquals(val, 'this-is-the-site-name')

def test_target_site_display_name(self):
val = self.migration.target_site_display_name
self.assertEquals(val, 'SharePoint Online Site Display Name')

def test_target_site_url(self):
val = self.migration.target_site_url
self.assertEquals(val, 'https://org.sharepoint.com/sites/this-is-the-site-name')

def test_job_status(self):
val = self.migration.job_status
self.assertEquals(val, 'Waiting to scan source data')
1 change: 0 additions & 1 deletion GoogleSharePointMigrationAssistant/web/views/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def get_context_data(self, **kwargs):
next_url = "/"
context['next'] = mark_safe(next_url)
return context
# POST

def form_valid(self, form):
username = form.cleaned_data['username']
Expand Down

0 comments on commit 95fa0ab

Please sign in to comment.