2017-01-11 01:53:10 +03:00
|
|
|
import json
|
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
from django.test import TestCase, Client
|
|
|
|
from django.contrib.auth import get_user_model
|
|
|
|
from django.contrib.auth.models import Group, Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2016-11-19 01:09:08 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
from pulseapi.entries.models import Entry, ModerationState
|
|
|
|
from pulseapi.entries.test_models import EntryFactory
|
|
|
|
|
|
|
|
from pulseapi.users.models import EmailUser
|
|
|
|
from pulseapi.users.test_models import EmailUserFactory
|
2017-05-03 21:06:26 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
from pulseapi.issues.models import Issue
|
2017-05-03 21:06:26 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
from pulseapi.utility.userpermissions import (
|
|
|
|
is_staff_address,
|
|
|
|
assign_group_policy,
|
|
|
|
add_user_to_main_site,
|
|
|
|
)
|
2017-05-03 21:06:26 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
def setup_groups():
|
|
|
|
staff, created = Group.objects.get_or_create(name='staff')
|
|
|
|
content_type = ContentType.objects.get_for_model(Entry)
|
2017-05-03 21:06:26 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
permission = Permission.objects.create(
|
|
|
|
codename='can_add_entry',
|
|
|
|
name='Can Add Entries',
|
|
|
|
content_type=content_type
|
|
|
|
)
|
|
|
|
staff.permissions.add(permission)
|
|
|
|
staff.save()
|
|
|
|
|
|
|
|
permission = Permission.objects.create(
|
|
|
|
codename='can_change_entry',
|
|
|
|
name='Can Change Entries',
|
|
|
|
content_type=content_type
|
|
|
|
)
|
|
|
|
staff.permissions.add(permission)
|
|
|
|
staff.save()
|
|
|
|
|
|
|
|
permission = Permission.objects.create(
|
|
|
|
codename='can_delete_entry',
|
|
|
|
name='Can Delete Entries',
|
|
|
|
content_type=content_type
|
|
|
|
)
|
|
|
|
staff.permissions.add(permission)
|
|
|
|
staff.save()
|
|
|
|
|
|
|
|
|
|
|
|
def setup_entries(test):
|
|
|
|
test.entries = [EntryFactory() for i in range(2)]
|
|
|
|
for entry in test.entries:
|
|
|
|
entry.save()
|
|
|
|
|
|
|
|
|
|
|
|
def create_logged_in_user(test, name, email, password="password1234"):
|
|
|
|
test.name = name
|
|
|
|
|
|
|
|
# create use instance
|
|
|
|
User = EmailUser
|
2017-08-10 08:22:43 +03:00
|
|
|
user = User.objects.create_user(name=name, email=email, password=password)
|
2017-06-16 20:45:45 +03:00
|
|
|
user.save()
|
2017-05-03 21:06:26 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
# make sure this user is in the staff group, too
|
|
|
|
if is_staff_address(email):
|
|
|
|
assign_group_policy(user, "staff")
|
|
|
|
add_user_to_main_site(user)
|
|
|
|
|
|
|
|
# log this user in for further testing purposes
|
|
|
|
test.user = user
|
|
|
|
test.client = Client()
|
|
|
|
test.client.force_login(user)
|
|
|
|
|
|
|
|
|
|
|
|
def generate_default_payload(values):
|
|
|
|
return {
|
|
|
|
'title': 'default title',
|
|
|
|
'nonce': values['nonce'],
|
|
|
|
'csrfmiddlewaretoken': values['csrf_token'],
|
|
|
|
'content_url': 'http://example.com/',
|
|
|
|
'tags': ['tag1', 'tag2']
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def generate_payload(test, data={}, payload=False):
|
|
|
|
values = json.loads(
|
|
|
|
str(test.client.get('/api/pulse/nonce/').content, 'utf-8')
|
|
|
|
)
|
|
|
|
|
|
|
|
if payload is False:
|
|
|
|
payload = generate_default_payload(values)
|
|
|
|
|
|
|
|
for key in data:
|
|
|
|
payload[key] = data[key]
|
|
|
|
|
|
|
|
return payload
|
|
|
|
|
|
|
|
|
|
|
|
def boostrap(test, name, email):
|
|
|
|
setup_groups()
|
|
|
|
create_logged_in_user(
|
|
|
|
test,
|
|
|
|
name=name,
|
|
|
|
email=email
|
|
|
|
)
|
|
|
|
setup_entries(test)
|
2017-05-03 21:06:26 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PulseMemberTestCase(TestCase):
|
2017-06-16 20:45:45 +03:00
|
|
|
"""
|
|
|
|
A test case wrapper for "plain users" without any staff or admin rights
|
|
|
|
"""
|
2017-01-11 01:53:10 +03:00
|
|
|
def setUp(self):
|
2017-06-16 20:45:45 +03:00
|
|
|
boostrap(
|
|
|
|
self,
|
|
|
|
name="plain user",
|
|
|
|
email="test@example.org"
|
|
|
|
)
|
2017-01-11 01:53:10 +03:00
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
def generatePostPayload(self, data={}):
|
|
|
|
return generate_payload(self, data)
|
2017-01-11 01:53:10 +03:00
|
|
|
|
|
|
|
|
2017-06-16 20:45:45 +03:00
|
|
|
class PulseStaffTestCase(TestCase):
|
|
|
|
"""
|
|
|
|
A test case wrapper for "staff" users, due to having a mozilla login
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
|
|
|
boostrap(
|
|
|
|
self,
|
|
|
|
name="staff user",
|
|
|
|
email="test@mozilla.org"
|
|
|
|
)
|
2017-01-11 01:53:10 +03:00
|
|
|
|
|
|
|
def generatePostPayload(self, data={}):
|
2017-06-16 20:45:45 +03:00
|
|
|
return generate_payload(self, data)
|