Commit 07f5d988 authored by nano's avatar nano

started test driven development

parent 78d0b1cb
Pipeline #46 failed with stage
in 56 seconds
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from fiex.settings import *
DEBUG=True
......
"""
Django settings for fiex project.
Generated by 'django-admin startproject' using Django 2.1.3.
For more information on this file, see
https://docs.djangoproject.com/en/2.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.1/ref/settings/
"""
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
......
"""fiex URL Configuration
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/2.1/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import include, path
......
......@@ -7,6 +7,23 @@ For more information on this file, see
https://docs.djangoproject.com/en/2.1/howto/deployment/wsgi/
"""
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
from django.core.wsgi import get_wsgi_application
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from django.contrib import admin
from django.urls import path
from django.http import HttpResponse
from .models import Exchange, File
@admin.register(Exchange)
......@@ -18,13 +36,15 @@ class ExchangeAdmin(admin.ModelAdmin):
def get_urls(self):
urls = super().get_urls()
my_urls = [
path('', self.my_view),
path('', self.exchange_view),
]
return my_urls + urls
def my_view(self, request):
pass
def exchange_view(self, request):
return HttpResponse('exchanges admin page')
def exchange_edit(self, request):
return HttpResponse('editing an exchange object')
fieldsets = [
(None, {'fields' : ['password','created_by']}),
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from django.apps import AppConfig
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by Django 2.1.3 on 2018-11-17 14:31
from django.db import migrations, models
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by Django 2.1.3 on 2018-11-20 20:53
from django.db import migrations, models
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by Django 2.1.3 on 2018-11-20 20:59
from django.db import migrations, models
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by Django 2.1.3 on 2018-11-20 21:13
from django.db import migrations, models
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Generated by Django 2.1.3 on 2018-11-20 21:27
from django.conf import settings
......
# Generated by Django 2.1.3 on 2018-11-22 20:32
import datetime
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('fiexapp', '0005_auto_20181120_2127'),
]
operations = [
migrations.CreateModel(
name='Permission',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=20, verbose_name='name of the permission')),
('description', models.CharField(max_length=300, verbose_name='description of the permission')),
],
),
migrations.CreateModel(
name='PermissionGranted',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date_granted', models.DateField(default=datetime.datetime(2018, 11, 22, 20, 32, 25, 56748, tzinfo=utc), null=True)),
],
),
migrations.AlterField(
model_name='exchange',
name='created_by',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='created_by', to=settings.AUTH_USER_MODEL),
),
migrations.AlterField(
model_name='exchange',
name='password',
field=models.CharField(max_length=100, null=True, verbose_name='optional password'),
),
migrations.AlterField(
model_name='exchange',
name='token',
field=models.CharField(default='lvrtmvkodrjw', editable=False, max_length=100, unique=True, verbose_name='access token'),
),
migrations.AddField(
model_name='permissiongranted',
name='exchange',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='fiexapp.Exchange'),
),
migrations.AddField(
model_name='permissiongranted',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='exchange',
name='permissions',
field=models.ManyToManyField(related_name='PermissionGranted', through='fiexapp.PermissionGranted', to=settings.AUTH_USER_MODEL),
),
]
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import string
import random
from django.db import models
from django.contrib.auth.models import User
from django.contrib.auth.hashers import make_password, is_password_usable
from django.utils import timezone
def gen_random_token(length=12, chars=string.ascii_lowercase + string.digits):
return ''.join(random.choice(chars) for _i in range(length))
def hash_password(password):
return make_password(password)
......@@ -22,10 +41,12 @@ class Exchange(models.Model):
"""
safety
"""
token = models.CharField('access token', max_length=100,unique=True, default=gen_random_token(DEFAULT_TOKEN_LENGTH),editable=False)
token = models.CharField('access token', max_length=100, unique=True,
default=gen_random_token(DEFAULT_TOKEN_LENGTH), editable=False)
# TODO securely safe password!
password = models.CharField('optional password', max_length=100, null=True)
created_by = models.ForeignKey(User,on_delete=models.CASCADE)
created_by = models.ForeignKey(User, on_delete=models.CASCADE,related_name="created_by")
permissions = models.ManyToManyField(User, through="PermissionGranted",related_name="PermissionGranted")
"""
description
......@@ -33,6 +54,11 @@ class Exchange(models.Model):
description = models.CharField('description', max_length=300)
creation_date = models.DateTimeField('date created')
def has_permission(self, user, permission):
raise NotImplementedError()
def change_permission(self, user, permission, granted):
raise NotImplementedError()
class File(models.Model):
......@@ -43,3 +69,20 @@ class File(models.Model):
path = models.CharField('path stored', max_length=200)
description = models.CharField('description of file', max_length=300, null=True)
Exchange = models.ForeignKey(Exchange, on_delete=models.CASCADE)
class Permission(models.Model):
"""
A model representing a permission. A user or a group can have a permission to do something on a special exchange.
"""
name = models.CharField('name of the permission',max_length=20)
description = models.CharField('description of the permission',max_length=300)
class PermissionGranted(models.Model):
"""
A model representing the permission relation between an User and an Exchange
"""
user = models.ForeignKey(User, on_delete=models.CASCADE)
exchange = models.ForeignKey(Exchange, on_delete=models.CASCADE)
date_granted = models.DateField(null=True, default=timezone.now())
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import re
from django.test import TestCase
from fiexapp.models import Exchange,File, gen_random_token, hash_password
from django.utils import timezone
from django.contrib.auth.hashers import check_password
from django.contrib.auth.models import User
from fiexapp import WRITE_PERMISSION, CHANGE_PERMISSION, EDIT_PERMISSION, DELETE_PERMISSION, VIEW_PERMISSION
# Create your tests here.
......@@ -13,9 +30,18 @@ class ExchangeTestCase(TestCase):
self.test_user = User.objects.create_superuser(username='test',email='test@example.com',password='verysecretpass')
self.exchange_description = "Django Test Instance"
self.exchange_creation_date = timezone.now()
Exchange.objects.create(description=self.exchange_description,creation_date=self.exchange_creation_date,created_by=self.test_user)
self.test_user1 = User.objects.create(username="permtest1", email="permtest1@example.com",
password="verysecretpass")
self.test_user2 = User.objects.create(username="permtest2", email="permtest1@example.com",
password="verysecretpass")
self.test_user3 = User.objects.create(username="permtest3", email="permtest1@example.com",
password="verysecretpass")
self.test_exchange1 = Exchange.objects.create(description="permtest", creation_date=timezone.now(),
created_by=self.test_user1)
def test_object_creation(self):
Exchange.objects.create(description=self.exchange_description, creation_date=self.exchange_creation_date,
created_by=self.test_user)
test_exchange_object = Exchange.objects.get(description=self.exchange_description,creation_date=self.exchange_creation_date)
self.assertEqual(test_exchange_object.description,self.exchange_description)
self.assertEqual(test_exchange_object.creation_date, self.exchange_creation_date)
......@@ -31,4 +57,39 @@ class ExchangeTestCase(TestCase):
def test_hash_password(self):
password = "very1secretpass!"
hash = hash_password(password)
self.assertTrue(check_password(password,hash))
\ No newline at end of file
self.assertTrue(check_password(password,hash))
def test_has_permission(self):
self.assertTrue(self.test_exchange1.has_permission(self.test_user1, VIEW_PERMISSION))
self.assertTrue(self.test_exchange1.has_permission(self.test_user1, WRITE_PERMISSION))
self.assertTrue(self.test_exchange1.has_permission(self.test_user1, CHANGE_PERMISSION))
self.assertTrue(self.test_exchange1.has_permission(self.test_user1, EDIT_PERMISSION))
self.assertTrue(self.test_exchange1.has_permission(self.test_user1, DELETE_PERMISSION))
self.assertFalse(self.test_exchange1.has_permission(self.test_user2, WRITE_PERMISSION))
self.assertFalse(self.test_exchange1.has_permission(self.test_user2, CHANGE_PERMISSION))
self.assertFalse(self.test_exchange1.has_permission(self.test_user2, EDIT_PERMISSION))
self.assertFalse(self.test_exchange1.has_permission(self.test_user2, VIEW_PERMISSION))
self.assertFalse(self.test_exchange1.has_permission(self.test_user2, DELETE_PERMISSION))
def test_grant_permission(self):
self.test_exchange1.change_permission(self.test_user2,VIEW_PERMISSION,True)
self.assertTrue(self.test_exchange1.has_permission(self.test_user2, VIEW_PERMISSION))
self.test_exchange1.change_permission(self.test_user2, WRITE_PERMISSION, True)
self.assertTrue(self.test_exchange1.has_permission(self.test_user2, WRITE_PERMISSION))
self.test_exchange1.change_permission(self.test_user2, CHANGE_PERMISSION, True)
self.assertTrue(self.test_exchange1.has_permission(self.test_user2, CHANGE_PERMISSION))
self.test_exchange1.change_permission(self.test_user2, EDIT_PERMISSION, True)
self.assertTrue(self.test_exchange1.has_permission(self.test_user2, EDIT_PERMISSION))
self.test_exchange1.change_permission(self.test_user2, DELETE_PERMISSION, True)
self.assertTrue(self.test_exchange1.has_permission(self.test_user2, DELETE_PERMISSION))
class PermissionTestCase(TestCase):
def setUp(self):
pass
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from django.urls import path
from . import views
......
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
from django.shortcuts import render
from django.http import HttpResponse
......
#!/usr/bin/env python
# Copyright (c) David Leeuwestein 2018.
#
# Fiex project site: https://anonym-online.net/fiex
# Fiex projet mail: fiex@anonym-online.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import os
import sys
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment