开始重构测试

This commit is contained in:
virusdefender 2016-03-28 18:43:13 +08:00
parent c323ff2395
commit a120733121
9 changed files with 0 additions and 2115 deletions

View File

@ -1,27 +0,0 @@
# coding=utf-8
from django.conf.urls import include, url
from django.views.generic import TemplateView
from .tests import (LoginRequiredCBVTestWithArgs, LoginRequiredCBVTestWithoutArgs,
AdminRequiredCBVTestWithArgs, AdminRequiredCBVTestWithoutArgs,
SuperAdminRequiredCBVTestWithArgs, SuperAdminRequiredCBVTestWithoutArgs)
urlpatterns = [
url(r'^login_required_test/fbv/1/$', "account.tests.login_required_FBV_test_without_args"),
url(r'^login_required_test/fbv/(?P<problem_id>\d+)/$', "account.tests.login_required_FBC_test_with_args"),
url(r'^login_required_test/cbv/1/$', LoginRequiredCBVTestWithoutArgs.as_view()),
url(r'^login_required_test/cbv/(?P<problem_id>\d+)/$', LoginRequiredCBVTestWithArgs.as_view()),
url(r'^admin_required_test/fbv/1/$', "account.tests.admin_required_FBV_test_without_args"),
url(r'^admin_required_test/fbv/(?P<problem_id>\d+)/$', "account.tests.admin_required_FBC_test_with_args"),
url(r'^admin_required_test/cbv/1/$', AdminRequiredCBVTestWithoutArgs.as_view()),
url(r'^admin_required_test/cbv/(?P<problem_id>\d+)/$', AdminRequiredCBVTestWithArgs.as_view()),
url(r'^super_admin_required_test/fbv/1/$', "account.tests.super_admin_required_FBV_test_without_args"),
url(r'^super_admin_required_test/fbv/(?P<problem_id>\d+)/$', "account.tests.super_admin_required_FBC_test_with_args"),
url(r'^super_admin_required_test/cbv/1/$', SuperAdminRequiredCBVTestWithoutArgs.as_view()),
url(r'^super_admin_required_test/cbv/(?P<problem_id>\d+)/$', SuperAdminRequiredCBVTestWithArgs.as_view()),
url(r'^login/$', TemplateView.as_view(template_name="oj/account/login.html"), name="user_login_page"),
]

View File

@ -1,494 +0,0 @@
# coding=utf-8
import json
import time
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from django.http import HttpResponse
from django.contrib import auth
from rest_framework.test import APITestCase, APIClient
from rest_framework.views import APIView
from .models import User, SUPER_ADMIN, REGULAR_USER, ADMIN
from .decorators import login_required, admin_required, super_admin_required
class UserLoginTest(TestCase):
def test_login_page(self):
client = Client()
response = client.get(reverse("user_login_page"))
self.assertTemplateUsed(response, "oj/account/login.html")
def create_user(username="test", real_name="test_real_name", email="test@qq.com",
password="111111", admin_type=REGULAR_USER):
user = User.objects.create(username=username, real_name=real_name, email=email, admin_type=admin_type)
user.set_password(password)
user.save()
return user
def set_captcha(session):
session["_django_captcha_key"] = "aaaa"
session["_django_captcha_expires_time"] = time.time() + 10000
session.save()
class UserLoginAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_login_api")
self.user = create_user()
set_captcha(self.client.session)
def test_invalid_data(self):
data = {"username": "test"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_captcha(self):
error_data = {"username": "test", "password": "test11", "captcha": "1111"}
response = self.client.post(self.url, data=error_data)
self.assertEqual(response.data, {"code": 1, "data": u"验证码错误"})
def test_error_username_or_password(self):
error_data = {"username": "test", "password": "test11", "captcha": "aaaa"}
response = self.client.post(self.url, data=error_data)
self.assertEqual(response.data, {"code": 1, "data": u"用户名或密码错误"})
def test_login_successfully(self):
data = {"username": "test", "password": "111111", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"登录成功"})
class UsernameCheckTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("username_check_api")
create_user()
def test_invalid_data(self):
response = self.client.get(self.url, data={"username111": "testtest"})
self.assertEqual(response.status_code, 200)
def test_username_exists(self):
response = self.client.get(self.url, data={"username": "test"})
self.assertEqual(response.status_code, 400)
def test_username_does_not_exist(self):
response = self.client.get(self.url, data={"username": "testtest123"})
self.assertEqual(response.status_code, 200)
class EmailCheckTest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("email_check_api")
create_user()
def test_invalid_data(self):
response = self.client.get(self.url, data={"email000": "11@qq.com"})
self.assertEqual(response.status_code, 200)
def test_email_exists(self):
response = self.client.get(self.url, data={"email": "test@qq.com"})
self.assertEqual(response.status_code, 400)
def test_email_does_not_exist(self):
response = self.client.get(self.url, data={"email": "33testtest@qq.com"})
self.assertEqual(response.status_code, 200)
class UserRegisterAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_register_api")
set_captcha(self.client.session)
def test_invalid_data(self):
data = {"username": "test", "real_name": "TT"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_captcha(self):
data = {"username": "test", "real_name": "TT", "password": "qqqqqq", "email": "6060@qq.com", "captcha": "bbaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"验证码错误"})
def test_short_password(self):
data = {"username": "test", "real_name": "TT", "password": "qq", "email": "6060@qq.com", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_same_username(self):
create_user()
data = {"username": "test", "real_name": "ww", "password": "zzzzzzz", "email": "606fds0@qq.com", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"用户名已存在"})
def test_same_email(self):
create_user(username="test1", email="test1@qq.com")
data = {"username": "aa", "real_name": "ww", "password": "zzzzzzz", "email": "test1@qq.com", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该邮箱已被注册,请换其他邮箱进行注册"})
def test_register_successfully(self):
data = {"username": "cc", "real_name": "dd", "password": "xxxxxx", "email": "9090@qq.com", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"注册成功!"})
class UserChangePasswordAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_change_password_api")
create_user()
self.client.login(username="test",password="111111")
set_captcha(self.client.session)
def test_captcha(self):
data = {"old_password": "aaaccc", "new_password": "aaaddd", "captcha": "abba"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"验证码错误"})
def test_invalid_data(self):
data = {"new_password": "aaaddd", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_error_old_password(self):
data = {"old_password": "aaaccc", "new_password": "aaaddd", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"密码不正确,请重新修改!"})
def test_change_password_successfully(self):
data = {"old_password": "111111", "new_password": "aaaccc", "captcha": "aaaa"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"用户密码修改成功!"})
class UserAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("user_admin_api")
self.user = create_user(admin_type=SUPER_ADMIN)
self.client.login(username="test", password="111111")
def test_success_get_data(self):
self.assertEqual(self.client.get(self.url).data["code"], 0)
def test_super_admin_required(self):
create_user(username="test1", email="test1@qq.com", admin_type=ADMIN)
self.client.login(username="test1", password="111111")
self.assertEqual(json.loads(self.client.get(self.url, HTTP_X_REQUESTED_WITH="XMLHttpRequest").content),
{"code": 1, "data": u"请先登录"})
self.assertEqual(json.loads(self.client.put(self.url, HTTP_X_REQUESTED_WITH="XMLHttpRequest").content),
{"code": 1, "data": u"请先登录"})
# 这个拦截操作其实是 Middleware 完成的
create_user(username="test2", email="test2@qq.com")
self.client.login(username="test2", password="111111")
self.assertEqual(json.loads(self.client.get(self.url, HTTP_X_REQUESTED_WITH="XMLHttpRequest").content),
{"code": 1, "data": u"请先登录"})
self.assertEqual(json.loads(self.client.put(self.url, HTTP_X_REQUESTED_WITH="XMLHttpRequest").content),
{"code": 1, "data": u"请先登录"})
def test_put_invalid_data(self):
data = {"username": "test", "password": "testaa"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_user_does_not_exist(self):
data = {"id": 8888, "username": "test0", "real_name": "test00",
"password": "testaa", "email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该用户不存在!"})
def test_username_exists(self):
create_user(username="test1", email="test1@qq.com")
data = {"id": self.user.id, "username": "test1", "real_name": "test00",
"password": "testaa", "email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"昵称已经存在"})
def test_edit_user_without_changing_password(self):
data = {"id": self.user.id, "username": "test2", "real_name": "test00",
"email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_user_edit_with_changing_password(self):
data = {"id": self.user.id, "username": "test", "real_name": "test00", "password": "111111",
"email": "60@qq.com", "admin_type": "2"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
self.assertIsNotNone(auth.authenticate(username="test", password="111111"))
def test_search_user(self):
r = self.assertEqual(self.client.get(self.url + "?keyword=11").status_code, 200)
def test_error_admin_type(self):
response = self.client.get(self.url + "?admin_type=error")
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
class UserInfoAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('user_info_api')
self.user = create_user()
def test_get_data_successfully(self):
self.client.login(username="test", password="111111")
data = self.client.get(self.url).data
self.assertEqual(data["code"], 0)
self.assertEqual(data["data"]["username"], self.user.username)
def test_get_data_without_logging_in(self):
self.assertEqual(self.client.get(self.url, HTTP_X_REQUESTED_WITH="XMLHttpRequest").data["code"], 1)
@login_required
def login_required_FBV_test_without_args(request):
return HttpResponse("function based view test1")
@login_required
def login_required_FBC_test_with_args(request, problem_id):
return HttpResponse(problem_id)
class LoginRequiredCBVTestWithoutArgs(APIView):
@login_required
def get(self, request):
return HttpResponse("class based view login required test1")
class LoginRequiredCBVTestWithArgs(APIView):
@login_required
def get(self, request, problem_id):
return HttpResponse(problem_id)
class LoginRequiredDecoratorTest(TestCase):
urls = 'account.test_urls'
def setUp(self):
self.client = Client()
user = User.objects.create(username="test")
user.set_password("test")
user.save()
def test_fbv_without_args(self):
# 没登陆
response = self.client.get("/login_required_test/fbv/1/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/login_required_test/fbv/1/")
self.assertEqual(response.content, "function based view test1")
def test_fbv_with_args(self):
# 没登陆
response = self.client.get("/login_required_test/fbv/1024/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/login_required_test/fbv/1024/")
self.assertEqual(response.content, "1024")
def test_cbv_without_args(self):
# 没登陆
response = self.client.get("/login_required_test/cbv/1/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/login_required_test/cbv/1/")
self.assertEqual(response.content, "class based view login required test1")
def test_cbv_with_args(self):
# 没登陆
response = self.client.get("/login_required_test/cbv/1024/", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(json.loads(response.content), {"code": 1, "data": u"请先登录"})
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/login_required_test/cbv/1024/")
self.assertEqual(response.content, "1024")
@admin_required
def admin_required_FBV_test_without_args(request):
return HttpResponse("function based view test1")
@admin_required
def admin_required_FBC_test_with_args(request, problem_id):
return HttpResponse(problem_id)
class AdminRequiredCBVTestWithoutArgs(APIView):
@admin_required
def get(self, request):
return HttpResponse("class based view login required test1")
class AdminRequiredCBVTestWithArgs(APIView):
@admin_required
def get(self, request, problem_id):
return HttpResponse(problem_id)
class AdminRequiredDecoratorTest(TestCase):
urls = 'account.test_urls'
def setUp(self):
self.client = Client()
user = User.objects.create(username="test")
user.admin_type = 1
user.set_password("test")
user.save()
def test_fbv_without_args(self):
# 没登陆
response = self.client.get("/admin_required_test/fbv/1/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/admin_required_test/fbv/1/")
self.assertEqual(response.content, "function based view test1")
def test_fbv_with_args(self):
# 没登陆
response = self.client.get("/admin_required_test/fbv/1024/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/admin_required_test/fbv/1024/")
self.assertEqual(response.content, "1024")
def test_cbv_without_args(self):
# 没登陆
response = self.client.get("/admin_required_test/cbv/1/", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.data, {"code": 1, "data": u"请先登录"})
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/admin_required_test/cbv/1/")
self.assertEqual(response.content, "class based view login required test1")
def test_cbv_with_args(self):
# 没登陆
response = self.client.get("/admin_required_test/cbv/1024/", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.data, {"code": 1, "data": u"请先登录"})
# 登陆后
self.client.login(username="test", password="test")
response = self.client.get("/admin_required_test/cbv/1024/")
self.assertEqual(response.content, "1024")
@super_admin_required
def super_admin_required_FBV_test_without_args(request):
return HttpResponse("function based view test1")
@super_admin_required
def super_admin_required_FBC_test_with_args(request, problem_id):
return HttpResponse(problem_id)
class SuperAdminRequiredCBVTestWithoutArgs(APIView):
@super_admin_required
def get(self, request):
return HttpResponse("class based view login required test1")
class SuperAdminRequiredCBVTestWithArgs(APIView):
@super_admin_required
def get(self, request, problem_id):
return HttpResponse(problem_id)
class SuperAdminRequiredDecoratorTest(TestCase):
urls = 'account.test_urls'
def setUp(self):
self.client = Client()
create_user(admin_type=SUPER_ADMIN)
def test_fbv_without_args(self):
# 没登陆
response = self.client.get("/super_admin_required_test/fbv/1/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="111111")
response = self.client.get("/super_admin_required_test/fbv/1/")
self.assertEqual(response.content, "function based view test1")
def test_fbv_with_args(self):
# 没登陆
response = self.client.get("/super_admin_required_test/fbv/1024/")
self.assertRedirects(response, "/login/")
# 登陆后
self.client.login(username="test", password="111111")
response = self.client.get("/super_admin_required_test/fbv/1024/")
self.assertEqual(response.content, "1024")
def test_cbv_without_args(self):
# 没登陆
response = self.client.get("/super_admin_required_test/cbv/1/", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.data, {"code": 1, "data": u"请先登录"})
# 登陆后
self.client.login(username="test", password="111111")
response = self.client.get("/super_admin_required_test/cbv/1/")
self.assertEqual(response.content, "class based view login required test1")
def test_cbv_with_args(self):
# 没登陆
response = self.client.get("/super_admin_required_test/cbv/1024/", HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(response.data, {"code": 1, "data": u"请先登录"})
# 登陆后
self.client.login(username="test", password="111111")
response = self.client.get("/super_admin_required_test/cbv/10086/")
self.assertEqual(response.content, "10086")
class UserLogoutTest(TestCase):
def setUp(self):
self.client = Client()
create_user()
def test_logout_success(self):
self.client = Client()
self.client.login(username="test", password="111111")
response = self.client.get("/logout/")
self.assertEqual(response.status_code, 302)
class IndexPageTest(TestCase):
def setUp(self):
create_user()
self.client = Client()
def test_not_login_user(self):
self.assertTemplateUsed(self.client.get("/"), "oj/index.html")
def test_no_referer_redirect(self):
self.client.login(username="test", password="111111")
self.assertRedirects(self.client.get("/"), "/problems/")
def test_visit_with_referer(self):
self.client.login(username="test", password="111111")
self.assertTemplateUsed(self.client.get("/", HTTP_REFERER="/about/"), "oj/index.html")

View File

@ -1,10 +0,0 @@
# coding=utf-8
from django.conf.urls import include, url
from django.views.generic import TemplateView
urlpatterns = [
url(r'^admin/$', TemplateView.as_view(template_name="admin/admin.html"), name="admin_spa_page"),
url(r'^api/admin/test/$', "admin.tests.middleware_test_func"),
url(r'^login/$', TemplateView.as_view(template_name="oj/account/login.html"), name="user_login_page"),
]

View File

@ -1,87 +0,0 @@
# coding=utf-8
import json
from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from django.http import HttpResponse
from account.models import User
def middleware_test_func(request):
return HttpResponse(json.dumps({"code": 0}))
class AdminRequiredMiddlewareTest(TestCase):
urls = "admin.test_urls"
def setUp(self):
admin_user = User.objects.create(username="test", admin_type=0)
admin_user.set_password("test")
admin_user.save()
admin_user = User.objects.create(username="test1", admin_type=1)
admin_user.set_password("test")
admin_user.save()
super_admin_user = User.objects.create(username="test2", admin_type=2)
super_admin_user.set_password("test")
super_admin_user.save()
self.client = Client()
def test_need_admin_login(self):
url = "/admin/"
response = self.client.get(url)
self.assertRedirects(response, "/login/")
self.client.login(username="test", password="test")
response = self.client.get(url)
self.assertRedirects(response, "/login/")
self.client.logout()
self.client.login(username="test1", password="test")
response = self.client.get(url)
self.assertTemplateUsed(response, "admin/admin.html")
self.client.logout()
self.client.login(username="test2", password="test")
response = self.client.get(url)
self.assertTemplateUsed(response, "admin/admin.html")
def test_need_admin_login_ajax(self):
url = "/api/admin/test/"
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(json.loads(response.content), {"code": 1, "data": u"请先登录"})
self.client.login(username="test", password="test")
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(json.loads(response.content), {"code": 1, "data": u"请先登录"})
self.client.logout()
self.client.login(username="test1", password="test")
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(json.loads(response.content)["code"], 0)
self.client.logout()
self.client.login(username="test2", password="test")
response = self.client.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
self.assertEqual(json.loads(response.content)["code"], 0)
class AdminTemplateViewTest(TestCase):
def setUp(self):
super_admin_user = User.objects.create(username="test", admin_type=2)
super_admin_user.set_password("test")
super_admin_user.save()
self.client = Client()
self.client.login(username="test", password="test")
def test_file_exists(self):
response = self.client.get("/admin/template/index/index.html")
self.assertEqual(response.status_code, 200)
def test_file_does_not_exist(self):
response = self.client.get("/admin/template/index/index123.html")
self.assertEqual(response.status_code, 200)
self.assertHTMLEqual(response.content, u"模板不存在")

View File

@ -1,64 +0,0 @@
# coding=utf-8
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from rest_framework.test import APITestCase, APIClient
from account.models import User
from account.tests import create_user
from group.models import Group
from announcement.models import Announcement
from account.models import ADMIN, SUPER_ADMIN
class AnnouncementAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse("announcement_admin_api")
self.user1 = create_user(admin_type=SUPER_ADMIN)
self.user2 = create_user(username="test1", email="test1@qq.com", admin_type=SUPER_ADMIN)
self.announcement = Announcement.objects.create(title="bb",
content="BB",
created_by=self.user1)
def test_create_announcement_successfully(self):
self.client.login(username="test", password="111111")
data = {"title": "title0", "content": "content0"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"公告发布成功!"})
def test_edit_announcement_successfully(self):
self.client.login(username="test", password="111111")
data = {"id": self.announcement.id, "title": "11", "content": "22", "visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
class AnnouncementPageTest(TestCase):
def setUp(self):
self.client = Client()
user = create_user()
self.a1 = Announcement.objects.create(title="aa",
content="AA",
created_by=user,
visible=True,
)
self.a2 = Announcement.objects.create(title="bb",
content="BB",
created_by=User.objects.get(username="test"),
visible=False
)
def test_visit_announcement_successfully(self):
response = self.client.get('/announcement/' + str(self.a1.id) + "/")
self.assertTemplateUsed(response, "oj/announcement/announcement.html")
def test_announcement_does_not_exist(self):
response = self.client.get('/announcement/10086/')
self.assertTemplateUsed(response, "utils/error.html")
def test_visit_hidden_announcement(self):
response = self.client.get('/announcement/' + str(self.a2.id) + "/")
self.assertTemplateUsed(response, "utils/error.html")

View File

@ -1,619 +0,0 @@
# coding=utf-8
import json
from django.core.urlresolvers import reverse
from django.test import TestCase, Client
from rest_framework.test import APITestCase, APIClient
from account.models import User
from group.models import Group
from contest.models import Contest, ContestProblem
from .models import GROUP_CONTEST, PASSWORD_PROTECTED_CONTEST, PUBLIC_CONTEST
from account.models import REGULAR_USER, ADMIN, SUPER_ADMIN
class ContestAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('contest_admin_api')
user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
user1.set_password("testaa")
user1.save()
user2 = User.objects.create(username="test2", admin_type=ADMIN)
user2.set_password("testbb")
user2.save()
user3 = User.objects.create(username="test3", admin_type=REGULAR_USER)
user3.set_password("testcc")
user3.save()
self.group = Group.objects.create(name="group1", description="des0",
join_group_setting=0, visible=True,
admin=user2)
self.group2 = Group.objects.create(name="group2", description="des0",
join_group_setting=0, visible=True,
admin=user1)
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
self.group_contest = Contest.objects.create(title="titley", description="descriptiony", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
# 以下是比赛发布的测试
def test_invalid_format(self):
self.client.login(username="test1", password="testaa")
data = {"title": "test1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_global_contest_does_not_has_privileges(self):
self.client.login(username="test2", password="testbb")
data = {"title": "title0", "description": "description0", "mode": 1, "contest_type": PASSWORD_PROTECTED_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "password": "aabb", "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"只有超级管理员才可创建公开赛"})
def test_global_contest_password_exists(self):
self.client.login(username="test1", password="testaa")
data = {"title": "title0", "description": "description0", "mode": 1, "contest_type": PASSWORD_PROTECTED_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"此比赛为有密码的公开赛,密码不可为空"})
def test_group_contest_group_at_least_one(self):
self.client.login(username="test1", password="testaa")
data = {"title": "title0", "description": "description0", "mode": 1, "contest_type": GROUP_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"请至少选择一个小组"})
def test_global_contest_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"title": "title1", "description": "description1", "mode": 1, "contest_type": PASSWORD_PROTECTED_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "password": "aabb", "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_group_contest_super_admin_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"title": "title3", "description": "description3", "mode": 1, "contest_type": GROUP_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "groups": [self.group.id], "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_group_contest_admin_successfully(self):
self.client.login(username="test2", password="testbb")
data = {"title": "title6", "description": "description6", "mode": 2, "contest_type": GROUP_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "groups": [self.group.id], "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_time_error(self):
self.client.login(username="test1", password="testaa")
data = {"title": "title2", "description": "description2", "mode": 1, "contest_type": PASSWORD_PROTECTED_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T12:00:00.000Z",
"end_time": "2015-08-15T10:00:00.000Z", "password": "aabb", "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"比赛的开始时间不能晚于或等于比赛结束的时间"})
def test_contest_has_exists(self):
self.client.login(username="test1", password="testaa")
data = {"title": "titlex", "description": "descriptionx", "mode": 1, "contest_type": PASSWORD_PROTECTED_CONTEST,
"show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "password": "aabb", "visible": True}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"比赛名已经存在"})
# 以下是编辑比赛的测试
def test_put_invalid_data(self):
self.client.login(username="test1", password="testaa")
data = {"title": "test0", "description": "description0"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_contest_does_not_exist(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.global_contest.id + 10, "title": "title2", "description": "description2", "mode": 1,
"contest_type": PASSWORD_PROTECTED_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T12:00:00.000Z", "password": "aabb",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该比赛不存在!"})
def test_edit_global_contest_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.global_contest.id, "title": "titlez", "description": "descriptionz", "mode": 1,
"contest_type": PASSWORD_PROTECTED_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T13:00:00.000Z", "password": "aabb",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
self.assertEqual(response.data["data"]["title"], "titlez")
#self.assertEqual(response.data["data"]["end_time"], "2015-08-15T13:00:00Z")
def test_edit_group_contest_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.group_contest.id, "title": "titleyyy", "description": "descriptionyyyy", "mode": 1,
"contest_type": GROUP_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T13:00:00.000Z",
"groups": [self.group.id], "visible": False}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
self.assertEqual(response.data["data"]["title"], "titleyyy")
#self.assertEqual(response.data["data"]["end_time"], "2015-08-15T13:00:00Z")
self.assertEqual(response.data["data"]["visible"], False)
def test_edit_group_contest_unsuccessfully(self):
self.client.login(username="test2", password="testbb")
data = {"id": self.group_contest.id, "title": "titleyyy", "description": "descriptionyyyy", "mode": 1,
"contest_type": GROUP_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T13:00:00.000Z",
"groups": [self.group.id], "visible": False}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_edit_group_at_least_one(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.group_contest.id, "title": "titleyyy", "description": "descriptionyyyy", "mode": 1,
"contest_type": GROUP_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T13:00:00.000Z", "visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"请至少选择一个小组"})
def test_edit_contest_has_exists(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.global_contest.id, "title": "titley", "description": "descriptiony", "mode": 1,
"contest_type": PASSWORD_PROTECTED_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T12:00:00.000Z", "password": "aabb",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该比赛名称已经存在"})
def test_edit_global_contest_does_not_has_privileges(self):
self.client.login(username="test2", password="testbb")
data = {"id": self.global_contest.id, "title": "titlexxxxxxxxx", "description": "descriptionxxxxxx", "mode": 1,
"contest_type": PASSWORD_PROTECTED_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T10:00:00.000Z", "end_time": "2015-08-15T12:00:00.000Z", "password": "aabb",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"只有超级管理员才可创建公开赛"})
def test_edit_global_contest_password_exists(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.global_contest.id, "title": "title0", "description": "description0", "mode": 1,
"contest_type": PASSWORD_PROTECTED_CONTEST, "show_rank": True, "show_user_submission": True, "start_time": "2015-08-15T10:00:00.000Z",
"end_time": "2015-08-15T12:00:00.000Z", "visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"此比赛为有密码的公开赛,密码不可为空"})
def test_edit_time_error(self):
self.client.login(username="test1", password="testaa")
data = {"id": self.global_contest.id, "title": "titleaaaa", "description": "descriptionaaaaa", "mode": 1,
"contest_type": PASSWORD_PROTECTED_CONTEST, "show_rank": True, "show_user_submission": True,
"start_time": "2015-08-15T12:00:00.000Z", "end_time": "2015-08-15T10:00:00.000Z", "password": "aabb",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"比赛的开始时间不能晚于或等于比赛结束的时间"})
# 以下是比赛分页的测试
def test_get_data_successfully(self):
self.client.login(username="test1", password="testaa")
self.assertEqual(self.client.get(self.url).data["code"], 0)
def test_get_data_successfully_by_normal_admin(self):
self.client.login(username="test2", password="testbb")
self.assertEqual(self.client.get(self.url).data["code"], 0)
def test_keyword_contest(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url + "?visible=true")
self.assertEqual(response.data["code"], 0)
for item in response.data["data"]:
self.assertEqual(item["visible"], True)
def test_query_by_keyword(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url + "?keyword=title1")
self.assertEqual(response.data["code"], 0)
class ContestProblemAdminAPItEST(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('contest_problem_admin_api')
self.user = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.user2 = User.objects.create(username="test2", admin_type=ADMIN)
self.user2.set_password("testaa")
self.user2.save()
self.user3 = User.objects.create(username="test3", admin_type=ADMIN)
self.user3.set_password("testaa")
self.user3.save()
self.client.login(username="test1", password="testaa")
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
self.contest_problem = ContestProblem.objects.create(title="titlex",
description="descriptionx",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
hint="hint1",
created_by=User.objects.get(username="test1"),
contest=Contest.objects.get(title="titlex"),
sort_index="a")
# 以下是发布比赛题目的测试
def test_invalid_format(self):
data = {"title": "test1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_release_contest_problem_successfully(self):
data = {"title": "title2",
"description": "description2",
"input_description": "input_description2",
"output_description": "output_description2",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"hint": "hint1",
"sort_index": "b",
"contest_id": self.global_contest.id}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_contest_does_not_exists(self):
data = {"title": "titlezzzzzzzz",
"description": "descriptionzzzzzzzzzzz",
"input_description": "input_description2",
"output_description": "output_description2",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"hint": "hint1",
"sort_index": "b",
"contest_id": self.global_contest.id + 10}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"比赛不存在"})
# 以下是编辑比赛题目的测试
def test_invalid_data(self):
data = {"title": "test1"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_edit_problem_does_not_exist(self):
data = {"id": self.contest_problem.id + 1,
"title": "title2",
"description": "description2",
"input_description": "input_description2",
"output_description": "output_description2",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"hint": "hint1",
"sort_index": "b",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该比赛题目不存在!"})
def test_edit_problem_successfully(self):
data = {"id": self.contest_problem.id,
"title": "title2222222",
"description": "description22222222",
"input_description": "input_description2",
"output_description": "output_description2",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"hint": "hint1",
"sort_index": "b",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
# 以下是比赛题目分页的测试
def test_get_data_successfully(self):
self.client.login(username="test1", password="testaa")
self.assertEqual(self.client.get(self.url).data["code"], 0)
def test_get_data_unsuccessfully(self):
self.client.login(username="test1", password="testaa")
self.assertEqual(self.client.get(self.url+"?contest_id=12").data["code"], 1)
def test_keyword_contest(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url + "?visible=true")
self.assertEqual(response.data["code"], 0)
for item in response.data["data"]:
self.assertEqual(item["visible"], True)
def test_query_by_keyword(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url + "?keyword=title1")
self.assertEqual(response.data["code"], 0)
def test_query_contest_problem_does_not_exist(self):
data = {"contest_problem_id": 1000000}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"比赛题目不存在"})
def test_query_contest_problem_exists(self):
data = {"contest_problem_id": self.contest_problem.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_query_contest_problem_exists_by_contest_id(self):
self.client.login(username="test3", password="testaa")
response = self.client.get(self.url + "?contest_id=" + str(self.global_contest.id))
self.assertEqual(response.data["code"], 0)
self.assertEqual(len(response.data["data"]), 0)
def test_query_contest_problem_exists_by_normal_admin(self):
self.client.login(username="test2", password="testaa")
data = {"contest_problem_id": self.contest_problem.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_edit_problem_unsuccessfully_can_not_access(self):
self.client.login(username="test2", password="testaa")
data = {"id": self.contest_problem.id,
"title": "title2222222",
"description": "description22222222",
"input_description": "input_description2",
"output_description": "output_description2",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"hint": "hint1",
"sort_index": "b",
"visible": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
class ContestPasswordVerifyAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('contest_password_verify_api')
self.user = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.user2 = User.objects.create(username="test2", admin_type=ADMIN)
self.user2.set_password("testbb")
self.user2.save()
self.client.login(username="test1", password="testaa")
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
def test_invalid_format(self):
self.client.login(username="test2", password="testbb")
data = {"contest_id": self.global_contest.id}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_contest_does_not_exist(self):
self.client.login(username="test2", password="testbb")
data = {"contest_id": self.global_contest.id + 1, "password": "aacc"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"比赛不存在"})
def test_contest_password_verify_unsuccessfully(self):
self.client.login(username="test2", password="testbb")
data = {"contest_id": self.global_contest.id, "password": "aabb"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"密码错误"})
def test_contest_password_verify_successfully(self):
self.client.login(username="test2", password="testbb")
data = {"contest_id": self.global_contest.id, "password": "aacc"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
class ContestPageTest(TestCase):
# 单个比赛详情页的测试
def setUp(self):
self.client = Client()
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.client.login(username="test1", password="testaa")
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
def test_visit_contest_page_successfully(self):
response = self.client.get('/contest/1/')
self.assertEqual(response.status_code, 200)
def test_visit_contest_page_unsuccessfully(self):
response = self.client.get('/contest/10/')
self.assertTemplateUsed(response, "utils/error.html")
class ContestProblemPageTest(TestCase):
# 单个比赛题目详情页的测试
def setUp(self):
self.client = Client()
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.client.login(username="test1", password="testaa")
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
self.contest_problem = ContestProblem.objects.create(title="titlex",
description="descriptionx",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
hint="hint1",
created_by=User.objects.get(username="test1"),
contest=Contest.objects.get(title="titlex"),
sort_index="a")
def test_visit_contest_problem_page_successfully(self):
response = self.client.get('/contest/1/problem/1/')
self.assertEqual(response.status_code, 200)
def test_visit_contest_page_unsuccessfully(self):
response = self.client.get('/contest/10/')
self.assertTemplateUsed(response, "utils/error.html")
def test_visit_contest_submissions_page_successfully(self):
ContestSubmission.objects.create(user=self.user1,
contest=self.global_contest,
problem=self.contest_problem,
ac=True)
response = self.client.get('/contest/1/problem/1/submissions/')
self.assertEqual(response.status_code, 200)
class ContestProblemListPageTest(TestCase):
# 比赛题目列表的测试
def setUp(self):
self.client = Client()
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.client.login(username="test1", password="testaa")
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
self.contest_problem = ContestProblem.objects.create(title="titlex",
description="descriptionx",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
hint="hint1",
created_by=User.objects.get(username="test1"),
contest=Contest.objects.get(title="titlex"),
sort_index="a")
def test_visit_contest_problem_list_page_successfully(self):
response = self.client.get('/contest/1/problems/')
self.assertEqual(response.status_code, 200)
def test_visit_contest_problem_page_unsuccessfully(self):
response = self.client.get('/contest/1/problem/100/')
self.assertTemplateUsed(response, "utils/error.html")
class ContestListPageTest(TestCase):
# 以下是所有比赛列表页的测试
def setUp(self):
self.client = Client()
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.url = reverse('contest_list_page')
self.client.login(username="test1", password="testaa")
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
def test_visit_contest_list_page_successfully(self):
response = self.client.get('/contests/')
self.assertEqual(response.status_code, 200)
def test_visit_contest_list_page_unsuccessfully(self):
response = self.client.get('/contests/2/')
self.assertTemplateUsed(response, "utils/error.html")
def test_query_by_keyword(self):
response = self.client.get(self.url + "?keyword=title1")
self.assertEqual(response.status_code, 200)
def test_query_by_join_successfully(self):
response = self.client.get(self.url + "?join=True")
self.assertEqual(response.status_code, 200)
class ContestProblemMySubmissionListTest(TestCase):
# 以下是我比赛单个题目的提交列表的测试
def setUp(self):
self.client = Client()
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.user2 = User.objects.create(username="test2", admin_type=REGULAR_USER)
self.user2.set_password("testbb")
self.user2.save()
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PUBLIC_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-30T12:00:00.000Z",
created_by=User.objects.get(username="test1"))
self.contest_problem = ContestProblem.objects.create(title="titlex",
description="descriptionx",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
hint="hint1",
created_by=self.user1,
contest=self.global_contest,
sort_index="a")
def test_contestsList_page_not_exist(self):
self.client.login(username="test1", password="testaa")
response = self.client.get('/contest/1/submissions/999/')
self.assertTemplateUsed(response, "utils/error.html")

View File

@ -1,306 +0,0 @@
# coding=utf-8
import json
from django.core.urlresolvers import reverse
from rest_framework.test import APITestCase, APIClient
from account.models import User, REGULAR_USER, ADMIN, SUPER_ADMIN
from group.models import Group, UserGroupRelation, JoinGroupRequest
from django.test import TestCase, Client
class GroupAPITest(APITestCase):
pass
class GroupAdminAPITest(APITestCase):
def _create_group(self, name, join_group_setting):
group = Group.objects.create(name=name, description="des0",
join_group_setting=join_group_setting, visible=True,
admin=self.user)
return group
def setUp(self):
self.client = APIClient()
self.url = reverse('group_admin_api')
self.user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.group = self._create_group("group1", 0)
self.client.login(username="test", password="testaa")
# 以下是创建小组的测试
def test_invalid_format(self):
data = {"name": "group1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_create_group_successfully(self):
data = {"name": "group0", "description": "des0", "join_group_setting": "1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_group_already_exists(self):
data = {"name": "group1", "description": "des0", "join_group_setting": "1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"小组名已经存在"})
# 以下是修改小组的测试
def test_put_invalid_data(self):
data = {"name": "group1"}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_edit_group_does_not_exist(self):
data = {"group_id": self.group.id + 1, "name": "group0", "description": "des0",
"join_group_setting": 2}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"小组不存在"})
def test_edit_group_successfully(self):
data = {"group_id": self.group.id, "name": "group0", "description": "des0",
"join_group_setting": 2}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 0)
self.assertEqual(response.data["data"]["name"], "group0")
self.assertEqual(response.data["data"]["join_group_setting"], 2)
def test_edit_group_exists(self):
group = self._create_group("group2", 1)
data = {"group_id": group.id, "name": "group1", "description": "des0",
"join_group_setting": 0}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"小组名已经存在"})
# 以下是查询小组列表或者是单个小组时的测试
def test_select_group_does_not_exist(self):
data = {"group_id": self.group.id + 1}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"小组不存在"})
def test_select_group_successfully(self):
data = {"group_id": self.group.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def tests_get_all_groups_successfully(self):
self.assertEqual(self.client.get(self.url).data["code"], 0)
class GroupMemberAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('group_member_admin_api')
self.user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.user1 = User.objects.create(username="member1", admin_type=REGULAR_USER)
self.user1.set_password("testxx")
self.user1.save()
self.client.login(username="test", password="testaa")
self.group = Group.objects.create(name="group1", description="des1",
join_group_setting="1", visible="True",
admin=self.user)
UserGroupRelation.objects.create(group=self.group, user=self.user1)
# 以下是查询小组成员的测试
def test_missing_parameter(self):
self.assertEqual(self.client.get(self.url).data, {"code": 1, "data": u"参数错误"})
def test_group_does_not_exist(self):
data = {"group_id": self.group.id + 1}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"小组不存在"})
def test_get_member_list_successfully(self):
data = {"group_id": self.group.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)
# 以下是删除小组成员的测试
def test_invalid_format(self):
data = {"members": [self.user1.id]}
response = self.client.put(self.url, data=json.dumps(data), content_type="application/json")
self.assertEqual(response.data["code"], 1)
def test_del_group_does_not_exist(self):
data = {"group_id": self.group.id + 1, "members": [self.user1.id]}
response = self.client.put(self.url, data=json.dumps(data), content_type="application/json")
self.assertEqual(response.data, {"code": 1, "data": u"小组不存在"})
def test_del_members_successfully(self):
data = {"group_id": self.group.id, "members": [self.user1.id]}
response = self.client.put(self.url, data=json.dumps(data), content_type="application/json")
self.assertEqual(response.data, {"code": 0, "data": u"删除成功"})
try:
UserGroupRelation.objects.get(group=self.group, user=self.user1)
raise AssertionError()
except UserGroupRelation.DoesNotExist:
pass
class JoinGroupAPITest(APITestCase):
def _create_group(self, name, join_group_setting):
group = Group.objects.create(name=name, description="des0",
join_group_setting=join_group_setting, visible="True",
admin=self.user)
return group
def setUp(self):
self.client = APIClient()
self.url = reverse('group_join_api')
self.user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.client.login(username="test", password="testaa")
self.group = self._create_group("group0", 0)
# 以下是用户要加入某个小组的测试
def test_invalid_format(self):
data = {"message": "message1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_group_does_not_exist(self):
data = {"group_id": self.group.id + 1, "message": "message1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"小组不存在"})
def test_join0_successfully(self):
data = {"group_id": self.group.id, "message": "message0"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"你已经成功的加入该小组"})
# 再加入一遍 已经是小组成员了
data = {"group_id": self.group.id, "message": "message0"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"你已经是小组成员了"})
def test_join1_successfully(self):
group = self._create_group("group1", 1)
data = {"group_id": group.id, "message": "message1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"申请提交成功,请等待审核"})
JoinGroupRequest.objects.get(user=self.user, group=group, status=False)
# 再提交一遍 已经提交过申请,请等待审核
data = {"group_id": group.id, "message": "message1"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"你已经提交过申请了,请等待审核"})
def test_join2_successfully(self):
group = self._create_group("group2", 2)
data = {"group_id": group.id, "message": "message2"}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"该小组不允许任何人加入"})
# 以下是搜索小组的测试
def test_error_get_data(self):
self.assertEqual(self.client.get(self.url).data["code"], 1)
def test_query_by_keyword(self):
response = self.client.get(self.url + "?keyword=group0")
self.assertEqual(response.data["code"], 0)
class JoinGroupRequestAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('join_group_request_admin_api')
self.user = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.user1 = User.objects.create(username="test2")
self.user1.set_password("testbb")
self.user1.save()
self.client.login(username="test1", password="testaa")
self.group = Group.objects.create(name="group1", description="des0",
join_group_setting=1, visible="True",
admin=self.user)
self.request = JoinGroupRequest.objects.create(group=self.group, user=self.user1,
message="message1")
# 以下是管理的群的加群请求测试
def test_get_all_request_successfully(self):
self.assertEqual(self.client.get(self.url).data["code"], 0)
# 以下是同意或者拒绝加入小组请求的测试
def test_invalid_format(self):
data = {"requested_id": self.request.id}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_request_does_not_exist(self):
data = {"request_id": self.request.id + 1, "status": False}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"请求不存在"})
def test_request_refuse_successfully(self):
data = {"request_id": self.request.id, "status": False}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"已拒绝"})
self.assertEqual(JoinGroupRequest.objects.get(id=self.request.id).status, True)
def test_join_group_successfully(self):
data = {"request_id": self.request.id, "status": True, "": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 0, "data": u"加入成功"})
UserGroupRelation.objects.get(group=self.group, user=self.user1)
# 再加入一次,此时返回的消息应为 加入失败,已经在本小组内
request = JoinGroupRequest.objects.create(group=self.group, user=self.user1,
message="message2")
data = {"request_id": request.id, "status": True}
response = self.client.put(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"加入失败,已经在本小组内"})
class GroupListPageTest(TestCase):
def setUp(self):
self.client = Client()
self.url = reverse('group_list_page')
self.url_with_argument = reverse('group_page', kwargs={"page": 1})
self.user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.group = Group.objects.create(name="group1",
description="description1",
# 0是公开 1是需要申请后加入 2是不允许任何人加入
join_group_setting=1,
admin=User.objects.get(username="test"))
def get_group_list_page_successful(self):
self.client.login(username="test", password="testaa")
response = self.client.get(self.url)
self.assertEqual(response.status_coed, 200)
def get_group_list_page_successful_with_keyword(self):
self.client.login(username="test", password="testaa")
response = self.client.get(self.url + "?keyword=gro")
self.assertEqual(response.status_coed, 200)
def get_group_list_page_successful_with_page_argument(self):
self.client.login(username="test", password="testaa")
response = self.client.get(self.url_with_argument + "?keyword=gro")
self.assertEqual(response.status_coed, 200)
class GroupPageTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username="test", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.group = Group.objects.create(name="group1",
description="description1",
# 0是公开 1是需要申请后加入 2是不允许任何人加入
join_group_setting=1,
admin=User.objects.get(username="test"))
self.url = reverse('group_page', kwargs={"group_id": self.group.id})
def get_group_list_page_successful(self):
self.client.login(username="test", password="testaa")
response = self.client.get(self.url)
self.assertEqual(response.status_coed, 200)

View File

@ -1,177 +0,0 @@
# coding=utf-8
import json
from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from rest_framework.test import APITestCase, APIClient
from account.models import User, SUPER_ADMIN
from account.tests import create_user
from problem.models import Problem, ProblemTag
class ProblemPageTest(TestCase):
def setUp(self):
self.client = APIClient()
self.user = create_user()
self.client.login(username="test", password="111111")
self.problem = Problem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
source="source1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
difficulty=1,
hint="hint1",
created_by=User.objects.get(username="test"))
def test_visit_problem_successfully(self):
response = self.client.get('/problem/' + str(self.problem.id) + "/")
self.assertTemplateUsed(response, "oj/problem/problem.html")
def test_problem_does_not_exist(self):
response = self.client.get('/problem/3000/')
self.assertTemplateUsed(response, "utils/error.html")
class ProblemAdminTest(APITestCase):
def _create_data(self, problem_id, visible, tags):
data = {"id": problem_id,
"title": "title0",
"description": "description0",
"input_description": "input_description0",
"output_description": "output_description0",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"difficulty": "1",
"hint": "hint1",
"visible": visible,
"tags": tags}
return data
def setUp(self):
self.client = APIClient()
self.url = reverse("problem_admin_api")
self.user = create_user(admin_type=SUPER_ADMIN)
self.client.login(username="test", password="111111")
ProblemTag.objects.create(name="tag1")
ProblemTag.objects.create(name="tag2")
self.problem = Problem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
source="source1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
difficulty=1,
hint="hint1",
created_by=self.user)
def test_create_problem_successfully(self):
data = {"title": "title2",
"description": "description2",
"input_description": "input_description2",
"output_description": "output_description2",
"test_case_id": "1",
"source": "source1",
"samples": [{"input": "1 1", "output": "2"}],
"time_limit": "100",
"memory_limit": "1000",
"difficulty": "1",
"hint": "hint1",
"visible": True,
"tags": [1]}
response = self.client.post(self.url, data=json.dumps(data), content_type="application/json")
self.assertEqual(response.data["code"], 0)
def test_edit_problem_successfully(self):
tags = ProblemTag.objects.filter(id__in=[1])
self.problem.tags.add(*tags)
data = self._create_data(self.problem.id, True, [1, 2])
response = self.client.put(self.url, data=json.dumps(data), content_type="application/json")
self.assertEqual(response.data["code"], 0)
# 以下是题目分页的测试
def test_success_get_data(self):
self.assertEqual(self.client.get(self.url).data["code"], 0)
def test_query_by_keyword(self):
response = self.client.get(self.url + "?keyword=title1")
self.assertEqual(response.data["code"], 0)
def test_query_by_visible(self):
response = self.client.get(self.url + "?visible=true")
self.assertEqual(response.data["code"], 0)
for item in response.data["data"]:
self.assertEqual(item["visible"], True)
def test_query_problem_does_not_exist(self):
data = {"problem_id": 200}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"题目不存在"})
def test_query_existed_problem(self):
data = {"problem_id": self.problem.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)
class ProblemTagAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('problem_tag_admin_api')
self.user = create_user(admin_type=SUPER_ADMIN)
self.client.login(username="test", password="111111")
ProblemTag.objects.create(name="tag1")
# 以下是返回所有的问题的标签
def test_get_all_problem_tag_successfully(self):
self.assertEqual(self.client.get(self.url).data["code"], 0)
class ProblemListPageTest(TestCase):
def setUp(self):
self.client = Client()
self.url = reverse('problem_list_page', kwargs={"page": 1})
self.user = create_user(admin_type=SUPER_ADMIN)
self.client.login(username="test", password="111111")
ProblemTag.objects.create(name="tag1")
ProblemTag.objects.create(name="tag2")
self.problem = Problem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
source="source1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
difficulty=1,
hint="hint1",
created_by=self.user)
def test_problemListPage_not_exist(self):
response = self.client.get('/problems/999/')
self.assertTemplateUsed(response, "utils/error.html")
def test_query_by_keyword(self):
response = self.client.get(self.url + "?keyword=title1")
self.assertEqual(response.status_code, 200)
def test_query_by_tag_successfully(self):
response = self.client.get(self.url + "?tag=")
self.assertEqual(response.status_code, 200)
def test_tag_does_not_exists(self):
response = self.client.get(self.url + "?tag=xxxxxx")
self.assertTemplateUsed(response, "utils/error.html")

View File

@ -1,331 +0,0 @@
# coding=utf-8
import json
from django.test import TestCase, Client
from django.core.urlresolvers import reverse
from account.models import User, REGULAR_USER, ADMIN, SUPER_ADMIN
from problem.models import Problem
from contest.models import Contest, ContestProblem
from contest.models import GROUP_CONTEST, PUBLIC_CONTEST, PASSWORD_PROTECTED_CONTEST
from submission.models import Submission
from rest_framework.test import APITestCase, APIClient
class SubmissionsListPageTest(TestCase):
def setUp(self):
self.client = Client()
self.user = User.objects.create(username="gogoing", admin_type=REGULAR_USER)
self.user2 = User.objects.create(username="cool", admin_type=REGULAR_USER)
self.user2.set_password("666666")
self.user.set_password("666666")
self.user.save()
self.user2.save()
self.submission = Submission.objects.create(user_id=self.user.id,
language=1,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=1)
def test_visit_submissionsListPage_successfully(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/submissions/1/')
self.assertEqual(response.status_code, 200)
def test_visit_submissionsListPage_successfully_language_filter(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/submissions/?language=1')
self.assertEqual(response.status_code, 200)
def test_visit_submissionsListPage_successfully_result_filter(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/submissions/?result=1')
self.assertEqual(response.status_code, 200)
def test_visit_submissionsListPage_without_page_successfully(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/submissions/')
self.assertEqual(response.status_code, 200)
def test_submissionsListPage_does_not_exist(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/submissions/5/')
self.assertTemplateUsed(response, "utils/error.html")
def test_submissionsListPage_page_not_exist(self):
self.client.login(username="gogoing", password="666666")
response = self.client.get('/submissions/999/')
self.assertTemplateUsed(response, "utils/error.html")
def test_submissionsListPage_have_no_submission(self):
self.client.login(username="cool", password="666666")
response = self.client.get('/submissions/')
self.assertEqual(response.status_code, 200)
class SubmissionAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('submission_api')
self.user1 = User.objects.create(username="test1", admin_type=REGULAR_USER)
self.user1.set_password("testaa")
self.user1.save()
self.user2 = User.objects.create(username="test2", admin_type=SUPER_ADMIN)
self.user2.set_password("testbb")
self.user2.save()
self.problem = Problem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
source="source1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
difficulty=1,
hint="hint1",
created_by=User.objects.get(username="test2"))
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PUBLIC_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test2"))
self.submission = Submission.objects.create(user_id=self.user1.id,
language=1,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=self.problem.id)
def test_invalid_format(self):
self.client.login(username="test1", password="testaa")
data = {"language": 1}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_problem_does_not_exist(self):
self.client.login(username="test1", password="testaa")
data = {"language": 1, "code": '#include "stdio.h"\nint main(){\n\treturn 0;\n}',
"problem_id": self.problem.id + 10}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"题目不存在"})
def test_submission_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"language": 1, "code": '#include "stdio.h"\nint main(){\n\treturn 0;\n}',
"problem_id": self.problem.id}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_submission_does_not_exist(self):
self.client.login(username="test1", password="testaa")
data = {"submission_id": self.submission.id + "111"}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"提交不存在"})
def test_get_submission_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"submission_id": self.submission.id}
response = self.client.get(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_parameter_error(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url)
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
class SubmissionAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('submission_admin_api_view')
self.user = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user.set_password("testaa")
self.user.save()
self.client.login(username="test1", password="testaa")
self.problem = Problem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
source="source1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
difficulty=1,
hint="hint1",
created_by=self.user)
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PUBLIC_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=self.user)
self.submission = Submission.objects.create(user_id=self.user.id,
language=1,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=self.problem.id)
def test_invalid_format(self):
response = self.client.get(self.url)
self.assertEqual(response.data, {"code": 1, "data": u"参数错误"})
class SubmissionPageTest(TestCase):
# 单个题目的提交详情页
def setUp(self):
self.client = Client()
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.user2 = User.objects.create(username="test2", admin_type=ADMIN)
self.user2.set_password("testbb")
self.user2.save()
self.client.login(username="test1", password="testaa")
self.problem = Problem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
source="source1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
difficulty=1,
hint="hint1",
created_by=User.objects.get(username="test1"))
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PUBLIC_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=User.objects.get(username="test1"))
self.submission = Submission.objects.create(user_id=self.user1.id,
language=1,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=self.problem.id)
class ContestSubmissionAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('contest_submission_api')
self.user1 = User.objects.create(username="test1", admin_type=SUPER_ADMIN)
self.user1.set_password("testaa")
self.user1.save()
self.user2 = User.objects.create(username="test2", admin_type=REGULAR_USER)
self.user2.set_password("testbb")
self.user2.save()
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PUBLIC_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-30T12:00:00.000Z",
created_by=User.objects.get(username="test1"))
self.contest_problem = ContestProblem.objects.create(title="titlex",
description="descriptionx",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
hint="hint1",
created_by=User.objects.get(username="test1"),
contest=Contest.objects.get(title="titlex"),
sort_index="a")
# 以下是创建比赛的提交
def test_invalid_format(self):
self.client.login(username="test1", password="testaa")
data = {"contest_id": self.global_contest.id, "language": 1}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 1)
def test_contest_submission_successfully(self):
self.client.login(username="test1", password="testaa")
data = {"contest_id": self.global_contest.id, "problem_id": self.contest_problem.id,
"language": 1, "code": '#include "stdio.h"\nint main(){\n\treturn 0;\n}'}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data["code"], 0)
def test_contest_problem_does_not_exist(self):
self.client.login(username="test1", password="testaa")
data = {"contest_id": self.global_contest.id, "problem_id": self.contest_problem.id + 10,
"language": 1, "code": '#include "stdio.h"\nint main(){\n\treturn 0;\n}'}
response = self.client.post(self.url, data=data)
self.assertEqual(response.data, {"code": 1, "data": u"题目不存在"})
class ContestSubmissionAdminAPITest(APITestCase):
def setUp(self):
self.client = APIClient()
self.url = reverse('contest_submission_admin_api_view')
self.userA = User.objects.create(username="test1", admin_type=ADMIN)
self.userA.set_password("testaa")
self.userA.save()
self.userS = User.objects.create(username="test2", admin_type=SUPER_ADMIN)
self.userS.set_password("testbb")
self.userS.save()
self.global_contest = Contest.objects.create(title="titlex", description="descriptionx", mode=1,
contest_type=PASSWORD_PROTECTED_CONTEST, show_rank=True,
show_user_submission=True,
start_time="2015-08-15T10:00:00.000Z",
end_time="2015-08-15T12:00:00.000Z",
password="aacc", created_by=self.userS
)
self.problem = ContestProblem.objects.create(title="title1",
description="description1",
input_description="input1_description",
output_description="output1_description",
test_case_id="1",
sort_index="1",
samples=json.dumps([{"input": "1 1", "output": "2"}]),
time_limit=100,
memory_limit=1000,
hint="hint1",
contest=self.global_contest,
created_by=self.userS)
self.submission = Submission.objects.create(user_id=self.userA.id,
language=1,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=self.problem.id)
self.submissionS = Submission.objects.create(user_id=self.userS.id,
language=2,
code='#include "stdio.h"\nint main(){\n\treturn 0;\n}',
problem_id=self.problem.id)
def test_submission_contest_does_not_exist(self):
self.client.login(username="test2", password="testbb")
response = self.client.get(self.url + "?contest_id=99")
self.assertEqual(response.data["code"], 1)
def test_submission_contest_parameter_error(self):
self.client.login(username="test2", password="testbb")
response = self.client.get(self.url)
self.assertEqual(response.data["code"], 1)
def test_submission_access_denied(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url + "?problem_id=" + str(self.problem.id))
self.assertEqual(response.data["code"], 1)
def test_submission_access_denied_with_contest_id(self):
self.client.login(username="test1", password="testaa")
response = self.client.get(self.url + "?contest_id=" + str(self.global_contest.id))
self.assertEqual(response.data["code"], 1)
def test_get_submission_successfully(self):
self.client.login(username="test2", password="testbb")
response = self.client.get(
self.url + "?contest_id=" + str(self.global_contest.id) + "&problem_id=" + str(self.problem.id))
self.assertEqual(response.data["code"], 0)
def test_get_submission_successfully_problem(self):
self.client.login(username="test2", password="testbb")
response = self.client.get(self.url + "?problem_id=" + str(self.problem.id))
self.assertEqual(response.data["code"], 0)
def test_get_submission_problem_do_not_exist(self):
self.client.login(username="test2", password="testbb")
response = self.client.get(self.url + "?problem_id=9999")
self.assertEqual(response.data["code"], 1)