def handle(self, *args, **options):
        if not len(args):
            user = User.objects(username='******').first() or User(
                username='******').save()
            user.group = Group.objects(name='Administrator').first()
            user.set_password('password')
            location = Location.objects(type='district').first() or Location(
                name='Kampala', type='district').save()
            profile = UserProfile.objects(phone='N/A').first() or UserProfile(
                phone='N/A',
                name='Admin',
                location=location,
                email='*****@*****.**').save()
            profile.user = user
            profile.save()
        else:
            user = User.objects(username=args[0]).first() or User(
                username=args[0]).save()
            user.group = Group.objects(name='Administrator').first()
            user.set_password(args[1])
            location = Location.objects(name=args[4]).first() or Location(
                name=args[4], type='district').save()
            profile = UserProfile.objects(
                phone=args[5]).first() or UserProfile(
                    phone=args[5],
                    name=args[3],
                    location=location,
                    email=args[2]).save().save()
            profile.user = user
            profile.save()

        self.stdout.write('Successfully created superuser')
Exemple #2
0
 def login_user(self):
     user = User.objects.create(username='******',
                                email='*****@*****.**')
     user.group = Group.objects(name='Administrator').first()
     user.set_password('password')
     self.client.login(username='******', password='******')
     return user
Exemple #3
0
 def login_user_with_group(self, grp):
     group = Group.objects(name=grp.name).first()
     user = User.objects.create(username='******',
                                email='*****@*****.**',
                                group=group)
     user.set_password('password')
     self.client.login(username='******', password='******')
     return user
    def test_serializing_group_name(self):
        mobile_user_attr = self.mobile_user.copy()
        group = Group.objects().first()
        mobile_user_attr['user'] = User(username='******', password='******', group=group).save()
        mobile_user = UserProfile(**mobile_user_attr).save()

        serialized_object = UserProfileSerializer(mobile_user)
        self.assertEqual(str(group.id), serialized_object.data['group'])
 def test_new_password_is_sent_in_email(self, mock_send_mail, mock_make_password):
     message = "Andrew http://necoc.org.ug andrew blabla"
     mock_make_password.return_value = 'blabla'
     profile = UserProfile(name='Andrew', email='*****@*****.**')
     group = Group.objects().first()
     UserProfileService(profile).setup_new_user('andrew', str(group.id))
     mock_send_mail.assert_called_with('Your NECOC Account',
                                       message,
                                       settings.DEFAULT_FROM_EMAIL,
                                       ['*****@*****.**'])
    def test_post_with_group_associates_user_to_group(self):
        attr = self.mobile_user_to_post.copy()
        attr['username'] = '******'
        group = Group.objects().first()
        attr['group'] = str(group.id)
        response = self.client.post(self.API_ENDPOINT, data=attr)
        self.assertEqual(201, response.status_code)

        retrieved_user = User.objects(username='******').first()
        self.assertEqual(group, retrieved_user.group)
    def handle(self, *args, **options):
        if not len(args):
            user = User.objects(username='******').first() or User(username='******').save()
            user.group = Group.objects(name='Administrator').first()
            user.set_password('password')
            location = Location.objects(type='district').first() or Location(name='Kampala', type='district').save()
            profile = UserProfile.objects(phone='N/A').first() or UserProfile(phone='N/A', name='Admin', location=location, email='*****@*****.**').save()
            profile.user = user
            profile.save()
        else:
            user = User.objects(username=args[0]).first() or User(username=args[0]).save()
            user.group = Group.objects(name='Administrator').first()
            user.set_password(args[1])
            location = Location.objects(name=args[4]).first() or Location(name=args[4], type='district').save()
            profile = UserProfile.objects(phone=args[5]).first() or UserProfile(phone=args[5], name=args[3], location=location, email=args[2]).save().save()
            profile.user = user
            profile.save()

        self.stdout.write('Successfully created superuser')
    def test_post_with_group_associates_user_to_group(self):
        attr = self.mobile_user_to_post.copy()
        attr['username'] = '******'
        group = Group.objects().first()
        attr['group'] = str(group.id)
        response = self.client.post(self.API_ENDPOINT, data=attr)
        self.assertEqual(201, response.status_code)

        retrieved_user = User.objects(username='******').first()
        self.assertEqual(group, retrieved_user.group)
Exemple #9
0
    def test_serializing_group_name(self):
        mobile_user_attr = self.mobile_user.copy()
        group = Group.objects().first()
        mobile_user_attr['user'] = User(username='******',
                                        password='******',
                                        group=group).save()
        mobile_user = UserProfile(**mobile_user_attr).save()

        serialized_object = UserProfileSerializer(mobile_user)
        self.assertEqual(str(group.id), serialized_object.data['group'])
    def test_reset_password_saves_new_password(self, mock_send_email):
        district = Location(**dict(name='Kampala', parent=None, type='district'))
        district.save()
        profile = UserProfile(name='Andrew', email='*****@*****.**', location=district, phone='2570760540326')
        user = UserProfileService(profile).setup_new_user('andrew', str((Group.objects().first()).id))
        old_password = user.password
        profile.user = user

        UserProfileService(profile).reset_password()
        self.assertNotEqual(old_password, profile.user.password)
    def test_update_with_group_associates_user_to_new_group(self):
        attr = self.mobile_user_to_post.copy()
        attr['username'] = '******'
        group = Group.objects().first()
        attr['group'] = str(group.id)
        self.client.post(self.API_ENDPOINT, data=attr)

        retrieved_user = User.objects(username='******').first()
        retrieved_user_profile = UserProfile.objects(
            user=retrieved_user).first()
        new_group = Group.objects().all()[2]

        new_attr = self.mobile_user_to_post.copy()
        new_attr['username'] = '******'
        new_attr['location'] = str(new_attr['location'])
        new_attr['group'] = str(new_group.id)
        new_attr['id'] = str(retrieved_user_profile.id)

        url = self.API_ENDPOINT + str(retrieved_user_profile.id) + '/'
        response = self.client.post(url, data=new_attr)
        self.assertEqual(200, response.status_code)
        retrieved_user = User.objects(username='******').first()
        self.assertEqual(new_group, retrieved_user.group)
    def test_update_with_group_associates_user_to_new_group(self):
        attr = self.mobile_user_to_post.copy()
        attr['username'] = '******'
        group = Group.objects().first()
        attr['group'] = str(group.id)
        self.client.post(self.API_ENDPOINT, data=attr)

        retrieved_user = User.objects(username='******').first()
        retrieved_user_profile = UserProfile.objects(user=retrieved_user).first()
        new_group = Group.objects().all()[2]

        new_attr = self.mobile_user_to_post.copy()
        new_attr['username'] = '******'
        new_attr['location'] = str(new_attr['location'])
        new_attr['group'] = str(new_group.id)
        new_attr['id'] = str(retrieved_user_profile.id)

        url = self.API_ENDPOINT + str(retrieved_user_profile.id) + '/'
        response = self.client.post(url,
                                    data=new_attr)
        self.assertEqual(200, response.status_code)
        retrieved_user = User.objects(username='******').first()
        self.assertEqual(new_group, retrieved_user.group)
    def test_password_reset_sends_email(self, mock_send_mail, mock_make_password):
        message = "Andrew blabla http://necoc.org.ug [email protected]"
        mock_make_password.return_value = 'blabla'
        district = Location(**dict(name='Kampala', parent=None, type='district'))
        district.save()
        profile = UserProfile(name='Andrew', email='*****@*****.**', location=district, phone='2570760540326')
        user = UserProfileService(profile).setup_new_user('andrew', str((Group.objects().first()).id))
        profile.user = user

        UserProfileService(profile).reset_password()
        mock_send_mail.assert_any_call('NECOC Password Reset',
                                       message,
                                       "*****@*****.**",
                                       ['*****@*****.**'])
Exemple #14
0
 def post_save(self, obj, created=False):
     group_id = self.request.DATA.get('group', None)
     if(group_id):
         obj.user.group = Group.objects(id=group_id).first()
         obj.user.save()
     self.replace_image(obj)
 def test_associates_group_to_user(self, mock_send_mail, mock_make_password):
     mock_make_password.return_value = 'blabla'
     profile = UserProfile(name='Andrew', email='*****@*****.**')
     group = Group.objects().first()
     user = UserProfileService(profile).setup_new_user('andrew', str(group.id))
     self.assertEqual(group, user.group)
 def in_groups(u):
     if u.is_authenticated():
         groups = Group.objects(name__in=group_names)
         if bool(u.is_member_of(groups)) | u.is_superuser:
             return True
     return False
Exemple #17
0
 def list(self, request, *args, **kwargs):
     queryset = Group.objects()
     data = [GroupSerializer(query).data for query in queryset]
     return Response(data)
Exemple #18
0
 def login_user_with_group(self, grp):
     group = Group.objects(name=grp.name).first()
     user = User.objects.create(username='******', email='*****@*****.**', group=group)
     user.set_password('password')
     self.client.login(username='******', password='******')
     return user
Exemple #19
0
 def login_user(self):
     user = User.objects.create(username='******', email='*****@*****.**')
     user.group = Group.objects(name='Administrator').first()
     user.set_password('password')
     self.client.login(username='******', password='******')
     return user
Exemple #20
0
 def list(self, request, *args, **kwargs):
     queryset = Group.objects()
     data = [GroupSerializer(query).data for query in queryset]
     return Response(data)
 def test_setup_new_user_saves_new_password(self, mock_send_mail):
     profile = UserProfile(name='Andrew', email='*****@*****.**')
     group = Group.objects().first()
     user = UserProfileService(profile).setup_new_user('andrew', str(group.id))
     self.assertIsNotNone(user.password)