mirror of
https://github.com/haiwen/seahub.git
synced 2025-09-08 18:30:53 +00:00
Add basic group unit test
This commit is contained in:
@@ -32,7 +32,6 @@ class AvatarTestCase(TestCase):
|
||||
def setUp(self):
|
||||
self.testdatapath = os.path.join(os.path.dirname(__file__), "testdata")
|
||||
self.user = User.objects.create_user('lennon@thebeatles.com', 'testpassword', is_active=True)
|
||||
self.user.save()
|
||||
|
||||
response = self.client.post('/accounts/login/', {
|
||||
'username': 'lennon@thebeatles.com',
|
||||
|
@@ -20,7 +20,6 @@ class BaseTestCase(TestCase):
|
||||
"""
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user('lennon@thebeatles.com', 'testpassword', is_active=True)
|
||||
self.user.save()
|
||||
|
||||
def tearDown(self):
|
||||
self.user.delete()
|
||||
|
1
group/fixtures/groupmessage.json
Normal file
1
group/fixtures/groupmessage.json
Normal file
@@ -0,0 +1 @@
|
||||
[{"pk": 1, "model": "group.groupmessage", "fields": {"timestamp": "2012-06-27 11:24:30", "message": "hello", "group_id": 1, "from_email": "groupuser1@foo.com"}}]
|
@@ -1,70 +0,0 @@
|
||||
"""
|
||||
This file demonstrates two different styles of tests (one doctest and one
|
||||
unittest). These will both pass when you run "manage.py test".
|
||||
|
||||
Replace these with more appropriate tests for your application.
|
||||
"""
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
from django.test import TestCase, Client
|
||||
|
||||
from models import GroupMessage
|
||||
from seahub.base.accounts import User
|
||||
|
||||
class GroupTestCase(TestCase):
|
||||
"""
|
||||
Helper base class for all the follow test cases.
|
||||
"""
|
||||
def setUp(self):
|
||||
self.user = User.objects.create_user('lennon@thebeatles.com', 'testpassword', is_active=True)
|
||||
self.user.save()
|
||||
|
||||
response = self.client.post('/accounts/login/', {
|
||||
'username': 'lennon@thebeatles.com',
|
||||
'password': 'testpassword',
|
||||
})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
def tearDown(self):
|
||||
self.user.delete()
|
||||
|
||||
class GroupTest(GroupTestCase):
|
||||
def test_leave_msg(self):
|
||||
# Create a group msg
|
||||
now = datetime.now()
|
||||
self.msg = GroupMessage.objects.create(group_id=101,
|
||||
from_email='test@test.com',
|
||||
message='hello',
|
||||
timestamp=now)
|
||||
self.assertEqual(GroupMessage.objects.all().count(), 1)
|
||||
|
||||
def test_create_group(self):
|
||||
# Create valid group
|
||||
response = self.c.post('/groups/', {
|
||||
'group_name': 'test_group',
|
||||
})
|
||||
self.assertEqual(len(response.context['groups']), 1)
|
||||
|
||||
# Create invalid group
|
||||
response = self.c.post('/groups/', {
|
||||
'group_name': 'test_@group',
|
||||
})
|
||||
self.assertNotEqual(response.context['error_msg'], None)
|
||||
|
||||
def test_msg_reply(self):
|
||||
# Extra parameters to make this a Ajax style request.
|
||||
kwargs = {'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'}
|
||||
|
||||
# A valid message reply
|
||||
response = self.c.post('/group/reply/1/', {
|
||||
'message': 'hello',
|
||||
}, follow=True, **kwargs)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# A reply to invalid group message
|
||||
response = self.c.post('/group/reply/2/', {
|
||||
'message': 'hello',
|
||||
}, follow=True, **kwargs)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
1
group/tests/__init__.py
Normal file
1
group/tests/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from group.tests.tests import *
|
1
group/tests/testdata/large_message
vendored
Normal file
1
group/tests/testdata/large_message
vendored
Normal file
@@ -0,0 +1 @@
|
||||
发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发g
|
1
group/tests/testdata/valid_message
vendored
Normal file
1
group/tests/testdata/valid_message
vendored
Normal file
@@ -0,0 +1 @@
|
||||
发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发发
|
107
group/tests/tests.py
Normal file
107
group/tests/tests.py
Normal file
@@ -0,0 +1,107 @@
|
||||
from datetime import datetime
|
||||
import os
|
||||
|
||||
from django.test import TestCase, Client
|
||||
|
||||
from group.models import GroupMessage
|
||||
from base.accounts import User
|
||||
from notifications.models import UserNotification
|
||||
|
||||
class GroupTestCase(TestCase):
|
||||
"""
|
||||
Helper base class for all the follow test cases.
|
||||
"""
|
||||
def setUp(self):
|
||||
self.testdatapath = os.path.join(os.path.dirname(__file__), "testdata")
|
||||
self.user = User.objects.create_user('lennon@thebeatles.com', 'testpassword', is_active=True)
|
||||
|
||||
# Login user
|
||||
response = self.client.post('/accounts/login/', {
|
||||
'username': 'lennon@thebeatles.com',
|
||||
'password': 'testpassword',
|
||||
})
|
||||
self.assertEqual(response.status_code, 302)
|
||||
|
||||
def _delete_group(self):
|
||||
self.client.get('/group/1/?op=dismiss')
|
||||
|
||||
def tearDown(self):
|
||||
# self._delete_group()
|
||||
self.user.delete()
|
||||
|
||||
class CreateGroupTest(GroupTestCase):
|
||||
def test_invalid_group_name(self):
|
||||
response = self.client.post('/groups/', {
|
||||
'group_name': 'test_@group',
|
||||
})
|
||||
self.assertNotEqual(response.context['error_msg'], None)
|
||||
|
||||
# def test_valid_group_name(self):
|
||||
# response = self.client.post('/groups/', {
|
||||
# 'group_name': 'test_group',
|
||||
# })
|
||||
# self.assertEqual(len(response.context['groups']), 1)
|
||||
|
||||
class GroupMessageTest(GroupTestCase):
|
||||
def test_leave_blank_msg(self):
|
||||
response = self.client.post('/group/1/', {
|
||||
'message': '',
|
||||
})
|
||||
self.failUnlessEqual(GroupMessage.objects.all().count(), 0)
|
||||
|
||||
def test_leave_500_chars_msg(self):
|
||||
f = open(os.path.join(self.testdatapath, "valid_message"), "rb")
|
||||
message = f.read()
|
||||
response = self.client.post('/group/1/', {
|
||||
'message': message,
|
||||
})
|
||||
# Redirect only if it worked
|
||||
# self.assertEqual(response.status_code, 302)
|
||||
self.assertEqual(GroupMessage.objects.all().count(), 1)
|
||||
|
||||
def test_leave_501_chars_msg(self):
|
||||
f = open(os.path.join(self.testdatapath, "large_message"), "rb")
|
||||
message = f.read()
|
||||
response = self.client.post('/group/1/', {
|
||||
'message': message,
|
||||
})
|
||||
self.failUnlessEqual(GroupMessage.objects.all().count(), 0)
|
||||
|
||||
class ReplyMessageTest(GroupTestCase):
|
||||
fixtures = ['groupmessage.json']
|
||||
|
||||
def test_reply_message_fails_with_invalid_message_id(self):
|
||||
# Extra parameters to make this a Ajax style request.
|
||||
kwargs = {'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'}
|
||||
|
||||
# A reply to invalid group message
|
||||
response = self.client.post('/group/reply/2/', {
|
||||
'message': 'hello',
|
||||
}, follow=True, **kwargs)
|
||||
self.assertEqual(response.status_code, 400)
|
||||
|
||||
def test_reply_message_succeeds(self):
|
||||
# Extra parameters to make this a Ajax style request.
|
||||
kwargs = {'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'}
|
||||
|
||||
# A reply to valid group message
|
||||
response = self.client.post('/group/reply/1/', {
|
||||
'message': 'hello',
|
||||
}, follow=True, **kwargs)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
def test_reply_message_succeeds_and_notify_user(self):
|
||||
# Extra parameters to make this a Ajax style request.
|
||||
kwargs = {'HTTP_X_REQUESTED_WITH':'XMLHttpRequest'}
|
||||
|
||||
# A reply to valid group message
|
||||
response = self.client.post('/group/reply/1/', {
|
||||
'message': '@foo: hello',
|
||||
}, follow=True, **kwargs)
|
||||
self.assertEqual(response.status_code, 200)
|
||||
|
||||
# A notification to user
|
||||
self.assertEqual(len(UserNotification.objects.filter(to_user='groupuser1@foo.com')), 1)
|
||||
|
||||
def test_no_notification_when_at_user_not_in_group(self):
|
||||
pass
|
@@ -2,7 +2,7 @@
|
||||
|
||||
. setenv.sh
|
||||
|
||||
./manage.py test base -v 2
|
||||
./manage.py test avatar -v 2
|
||||
./manage.py test base avatar group -v 2
|
||||
|
||||
echo $?
|
||||
|
||||
|
Reference in New Issue
Block a user