# Path: lrs/models.py
# class Agent(models.Model):
#     objectType = models.CharField(max_length=6, blank=True, default="Agent")
#     name = models.CharField(max_length=100, blank=True)
#     mbox = models.CharField(
#         max_length=128, db_index=True, null=True, unique=True)
#     mbox_sha1sum = models.CharField(
#         max_length=40, db_index=True, null=True, unique=True)
#     openid = models.CharField(
#         max_length=MAX_URL_LENGTH, db_index=True, null=True, unique=True)
#     oauth_identifier = models.CharField(
#         max_length=192, db_index=True, null=True, unique=True)
#     member = models.ManyToManyField('self', related_name="agents")
#     account_homePage = models.CharField(max_length=MAX_URL_LENGTH, null=True)
#     account_name = models.CharField(max_length=50, null=True)
#     user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
#     objects = AgentManager()
# 
#     class Meta:
#         unique_together = ("account_homePage", "account_name")
# 
#     def to_dict(self, ids_only=False):
#         ret = OrderedDict()
#         if self.mbox:
#             ret['mbox'] = self.mbox
#         if self.mbox_sha1sum:
#             ret['mbox_sha1sum'] = self.mbox_sha1sum
#         if self.openid:
#             ret['openid'] = self.openid
#         if self.account_name:
#             ret['account'] = OrderedDict()
#             ret['account']['name'] = self.account_name
#             ret['account']['homePage'] = self.account_homePage
#         if self.objectType == 'Group':
#             ret['objectType'] = self.objectType
#             # show members for groups if ids_only is false
#             # show members' ids for anon groups if ids_only is true
#             if not ids_only or not (set(['mbox', 'mbox_sha1sum', 'openid', 'account']) & set(ret.keys())):
#                 if self.member.all():
#                     ret['member'] = [a.to_dict(ids_only)
#                                      for a in self.member.all()]
# 
#         ret['objectType'] = self.objectType
#         if self.name and not ids_only:
#             ret['name'] = self.name
#         return ret
# 
#     # Used only for /agent GET endpoint (check spec)
#     def to_dict_person(self):
#         ret = OrderedDict()
#         ret['objectType'] = "Person"
#         if self.name:
#             ret['name'] = [self.name]
#         if self.mbox:
#             ret['mbox'] = [self.mbox]
#         if self.mbox_sha1sum:
#             ret['mbox_sha1sum'] = [self.mbox_sha1sum]
#         if self.openid:
#             ret['openid'] = [self.openid]
#         if self.account_name:
#             ret['account'] = []
#             acc = OrderedDict()
#             acc['name'] = self.account_name
#             acc['homePage'] = self.account_homePage
#             ret['account'].append(acc)
#         return ret
# 
#     def get_a_name(self):
#         if self.name:
#             return self.name
#         if self.mbox:
#             return self.mbox
#         if self.mbox_sha1sum:
#             return self.mbox_sha1sum
#         if self.openid:
#             return self.openid
#         if self.account_name:
#             return self.account_name
#         if self.objectType == 'Agent':
#             return "unknown"
#         else:
#             return "anonymous group"
# 
#     def get_user_from_oauth_group(self):
#         if self.oauth_identifier:
#             if self.member.all()[0].account_homePage:
#                 return self.member.all()[1]
#             else:
#                 return self.member.all()[0]
#         return None
# 
#     def __unicode__(self):
#         return json.dumps(self.to_dict(), sort_keys=False)
# 
# Path: adl_lrs/views.py
# @csrf_protect
# @require_http_methods(["POST", "GET"])
# def register(request):
#     if request.method == 'GET':
#         form = RegisterForm()
#         return render(request, 'register.html', {"form": form})
#     elif request.method == 'POST':
#         form = RegisterForm(request.POST)
#         if form.is_valid():
#             name = form.cleaned_data['username']
#             pword = form.cleaned_data['password']
#             email = form.cleaned_data['email']
# 
#             # If username doesn't already exist
#             if not User.objects.filter(username__exact=name).count():
#                 # if email doesn't already exist
#                 if not User.objects.filter(email__exact=email).count():
#                     User.objects.create_user(name, email, pword)
#                 else:
#                     return render(request, 'register.html', {"form": form, "error_message": "Email %s is already registered." % email})
#             else:
#                 return render(request, 'register.html', {"form": form, "error_message": "User %s already exists." % name})
# 
#             # If a user is already logged in, log them out
#             if request.user.is_authenticated:
#                 logout(request)
# 
#             new_user = authenticate(username=name, password=pword)
#             login(request, new_user)
#             return HttpResponseRedirect(reverse('home'))
#         else:
#             return render(request, 'register.html', {"form": form})


import json
import base64
from django.test import TestCase
from django.urls import reverse
from django.conf import settings
from ..models import Agent
from adl_lrs.views import register





class AgentTests(TestCase):

    @classmethod
    def setUpClass(cls):
        print("\n%s" % __name__)
        super(AgentTests, cls).setUpClass()

    def setUp(self):
        self.username = "tester"
        self.password = "test"
        self.email = "test@example.com"
        self.auth = "Basic %s" % base64.b64encode(
            "%s:%s" % (self.username, self.password))
        form = {'username': self.username, 'password': self.password,
                'password2': self.password, 'email': self.email}

# Path: lrs/models.py
# class Agent(models.Model):
#     objectType = models.CharField(max_length=6, blank=True, default="Agent")
#     name = models.CharField(max_length=100, blank=True)
#     mbox = models.CharField(
#         max_length=128, db_index=True, null=True, unique=True)
#     mbox_sha1sum = models.CharField(
#         max_length=40, db_index=True, null=True, unique=True)
#     openid = models.CharField(
#         max_length=MAX_URL_LENGTH, db_index=True, null=True, unique=True)
#     oauth_identifier = models.CharField(
#         max_length=192, db_index=True, null=True, unique=True)
#     member = models.ManyToManyField('self', related_name="agents")
#     account_homePage = models.CharField(max_length=MAX_URL_LENGTH, null=True)
#     account_name = models.CharField(max_length=50, null=True)
#     user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True)
#     objects = AgentManager()
# 
#     class Meta:
#         unique_together = ("account_homePage", "account_name")
# 
#     def to_dict(self, ids_only=False):
#         ret = OrderedDict()
#         if self.mbox:
#             ret['mbox'] = self.mbox
#         if self.mbox_sha1sum:
#             ret['mbox_sha1sum'] = self.mbox_sha1sum
#         if self.openid:
#             ret['openid'] = self.openid
#         if self.account_name:
#             ret['account'] = OrderedDict()
#             ret['account']['name'] = self.account_name
#             ret['account']['homePage'] = self.account_homePage
#         if self.objectType == 'Group':
#             ret['objectType'] = self.objectType
#             # show members for groups if ids_only is false
#             # show members' ids for anon groups if ids_only is true
#             if not ids_only or not (set(['mbox', 'mbox_sha1sum', 'openid', 'account']) & set(ret.keys())):
#                 if self.member.all():
#                     ret['member'] = [a.to_dict(ids_only)
#                                      for a in self.member.all()]
# 
#         ret['objectType'] = self.objectType
#         if self.name and not ids_only:
#             ret['name'] = self.name
#         return ret
# 
#     # Used only for /agent GET endpoint (check spec)
#     def to_dict_person(self):
#         ret = OrderedDict()
#         ret['objectType'] = "Person"
#         if self.name:
#             ret['name'] = [self.name]
#         if self.mbox:
#             ret['mbox'] = [self.mbox]
#         if self.mbox_sha1sum:
#             ret['mbox_sha1sum'] = [self.mbox_sha1sum]
#         if self.openid:
#             ret['openid'] = [self.openid]
#         if self.account_name:
#             ret['account'] = []
#             acc = OrderedDict()
#             acc['name'] = self.account_name
#             acc['homePage'] = self.account_homePage
#             ret['account'].append(acc)
#         return ret
# 
#     def get_a_name(self):
#         if self.name:
#             return self.name
#         if self.mbox:
#             return self.mbox
#         if self.mbox_sha1sum:
#             return self.mbox_sha1sum
#         if self.openid:
#             return self.openid
#         if self.account_name:
#             return self.account_name
#         if self.objectType == 'Agent':
#             return "unknown"
#         else:
#             return "anonymous group"
# 
#     def get_user_from_oauth_group(self):
#         if self.oauth_identifier:
#             if self.member.all()[0].account_homePage:
#                 return self.member.all()[1]
#             else:
#                 return self.member.all()[0]
#         return None
# 
#     def __unicode__(self):
#         return json.dumps(self.to_dict(), sort_keys=False)
# 
# Path: adl_lrs/views.py
# @csrf_protect
# @require_http_methods(["POST", "GET"])
# def register(request):
#     if request.method == 'GET':
#         form = RegisterForm()
#         return render(request, 'register.html', {"form": form})
#     elif request.method == 'POST':
#         form = RegisterForm(request.POST)
#         if form.is_valid():
#             name = form.cleaned_data['username']
#             pword = form.cleaned_data['password']
#             email = form.cleaned_data['email']
# 
#             # If username doesn't already exist
#             if not User.objects.filter(username__exact=name).count():
#                 # if email doesn't already exist
#                 if not User.objects.filter(email__exact=email).count():
#                     User.objects.create_user(name, email, pword)
#                 else:
#                     return render(request, 'register.html', {"form": form, "error_message": "Email %s is already registered." % email})
#             else:
#                 return render(request, 'register.html', {"form": form, "error_message": "User %s already exists." % name})
# 
#             # If a user is already logged in, log them out
#             if request.user.is_authenticated:
#                 logout(request)
# 
#             new_user = authenticate(username=name, password=pword)
#             login(request, new_user)
#             return HttpResponseRedirect(reverse('home'))
#         else:
#             return render(request, 'register.html', {"form": form})


# Path: lrs/tests/test_Agent.py
import json
import base64
from django.test import TestCase
from django.urls import reverse
from django.conf import settings
from ..models import Agent
from adl_lrs.views import register






class AgentTests(TestCase):

    @classmethod
    def setUpClass(cls):
        print("\n%s" % __name__)
        super(AgentTests, cls).setUpClass()

    def setUp(self):
        self.username = "tester"
        self.password = "test"
        self.email = "test@example.com"
        self.auth = "Basic %s" % base64.b64encode(
            "%s:%s" % (self.username, self.password))
        form = {'username': self.username, 'password': self.password,
                'password2': self.password, 'email': self.email}

        self.client.post(reverse(register), form,