mirror of
https://github.com/haiwen/seahub.git
synced 2025-08-25 10:11:24 +00:00
Sending Links: conform api to web gui code
This commit is contained in:
parent
d85fdccddf
commit
fd6fd1eec0
@ -12,9 +12,10 @@ from seahub.api2.authentication import TokenAuthentication
|
||||
from seahub.api2.throttling import UserRateThrottle
|
||||
from seahub.api2.utils import api_error
|
||||
from seahub.utils import IS_EMAIL_CONFIGURED, is_valid_username, \
|
||||
string2list, gen_shared_link, send_html_email
|
||||
is_valid_email, string2list, gen_shared_link, send_html_email
|
||||
from seahub.share.models import FileShare
|
||||
from seahub.settings import REPLACE_FROM_EMAIL, ADD_REPLY_TO_HEADER, SITE_NAME
|
||||
from seahub.profile.models import Profile
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -60,11 +61,13 @@ class SendShareLinkView(APIView):
|
||||
result['failed'] = []
|
||||
result['success'] = []
|
||||
to_email_list = string2list(email)
|
||||
# use contact_email, if present
|
||||
useremail = Profile.objects.get_contact_email_by_user(request.user.username)
|
||||
for to_email in to_email_list:
|
||||
|
||||
failed_info = {}
|
||||
|
||||
if not is_valid_username(to_email):
|
||||
if not is_valid_email(to_email):
|
||||
failed_info['email'] = to_email
|
||||
failed_info['error_msg'] = 'email invalid.'
|
||||
result['failed'].append(failed_info)
|
||||
@ -78,12 +81,12 @@ class SendShareLinkView(APIView):
|
||||
}
|
||||
|
||||
if REPLACE_FROM_EMAIL:
|
||||
from_email = username
|
||||
from_email = useremail
|
||||
else:
|
||||
from_email = None # use default from email
|
||||
|
||||
if ADD_REPLY_TO_HEADER:
|
||||
reply_to = username
|
||||
reply_to = useremail
|
||||
else:
|
||||
reply_to = None
|
||||
|
||||
|
@ -11,9 +11,10 @@ from seahub.api2.authentication import TokenAuthentication
|
||||
from seahub.api2.throttling import UserRateThrottle
|
||||
from seahub.api2.utils import api_error
|
||||
from seahub.utils import IS_EMAIL_CONFIGURED, is_valid_username, \
|
||||
string2list, gen_shared_upload_link, send_html_email
|
||||
is_valid_email, string2list, gen_shared_upload_link, send_html_email
|
||||
from seahub.share.models import UploadLinkShare
|
||||
from seahub.settings import REPLACE_FROM_EMAIL, ADD_REPLY_TO_HEADER, SITE_NAME
|
||||
from seahub.profile.models import Profile
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@ -59,10 +60,13 @@ class SendUploadLinkView(APIView):
|
||||
result['failed'] = []
|
||||
result['success'] = []
|
||||
to_email_list = string2list(email)
|
||||
# use contact_email, if present
|
||||
useremail = Profile.objects.get_contact_email_by_user(request.user.username)
|
||||
for to_email in to_email_list:
|
||||
|
||||
failed_info = {}
|
||||
if not is_valid_username(to_email):
|
||||
|
||||
if not is_valid_email(to_email):
|
||||
failed_info['email'] = to_email
|
||||
failed_info['error_msg'] = 'email invalid.'
|
||||
result['failed'].append(failed_info)
|
||||
@ -76,12 +80,12 @@ class SendUploadLinkView(APIView):
|
||||
}
|
||||
|
||||
if REPLACE_FROM_EMAIL:
|
||||
from_email = username
|
||||
from_email = useremail
|
||||
else:
|
||||
from_email = None # use default from email
|
||||
|
||||
if ADD_REPLY_TO_HEADER:
|
||||
reply_to = username
|
||||
reply_to = useremail
|
||||
else:
|
||||
reply_to = None
|
||||
|
||||
|
@ -1,8 +1,14 @@
|
||||
#coding: UTF-8
|
||||
import json
|
||||
from mock import patch
|
||||
from django.core import mail
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import override_settings
|
||||
|
||||
from seahub.utils import IS_EMAIL_CONFIGURED
|
||||
from seahub.share.models import FileShare
|
||||
from seahub.profile.models import Profile
|
||||
from seahub.profile.utils import refresh_cache
|
||||
from seahub.test_utils import BaseTestCase
|
||||
|
||||
|
||||
@ -13,11 +19,15 @@ class SendShareLinkApiTest(BaseTestCase):
|
||||
self.repo.id, self.file, None)
|
||||
|
||||
self.token = fs.token
|
||||
mail.outbox = []
|
||||
|
||||
def tearDown(self):
|
||||
self.remove_repo()
|
||||
|
||||
def test_can_send_email(self):
|
||||
@override_settings(DEFAULT_FROM_EMAIL='from_noreply@seafile.com')
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.IS_EMAIL_CONFIGURED', True)
|
||||
def test_can_send_email_configured(self):
|
||||
self.login_as(self.user)
|
||||
invalid_email = 'invalid'
|
||||
url = reverse("api2-send-share-link")
|
||||
@ -27,13 +37,78 @@ class SendShareLinkApiTest(BaseTestCase):
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
if not IS_EMAIL_CONFIGURED:
|
||||
self.assertEqual(403, resp.status_code)
|
||||
else:
|
||||
self.assertEqual(200, resp.status_code)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert json_resp['failed'][0]['email'] == invalid_email
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert json_resp['failed'][0]['email'] == invalid_email
|
||||
assert 'test.txt' in mail.outbox[0].body
|
||||
assert mail.outbox[0].from_email == 'from_noreply@seafile.com'
|
||||
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.REPLACE_FROM_EMAIL', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.ADD_REPLY_TO_HEADER', True)
|
||||
def test_can_send_email_rewrite(self):
|
||||
self.login_as(self.user)
|
||||
url = reverse("api2-send-share-link")
|
||||
data = {
|
||||
"token": self.token,
|
||||
"email": self.admin.email,
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert 'test.txt' in mail.outbox[0].body
|
||||
assert mail.outbox[0].from_email == self.user.email
|
||||
assert mail.outbox[0].extra_headers['Reply-to'] == self.user.email
|
||||
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.REPLACE_FROM_EMAIL', True)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.ADD_REPLY_TO_HEADER', True)
|
||||
def test_can_send_email_rewrite_contact_email(self):
|
||||
self.login_as(self.user)
|
||||
nickname = 'Testuser'
|
||||
contact_email= 'contact_email@test.com'
|
||||
p = Profile.objects.add_or_update(self.user.email, nickname=nickname)
|
||||
p.contact_email = contact_email
|
||||
p.save()
|
||||
|
||||
refresh_cache(self.user.email)
|
||||
|
||||
url = reverse("api2-send-share-link")
|
||||
data = {
|
||||
"token": self.token,
|
||||
"email": self.admin.email,
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert 'test.txt' in mail.outbox[0].body
|
||||
assert mail.outbox[0].from_email == contact_email
|
||||
assert mail.outbox[0].extra_headers['Reply-to'] == contact_email
|
||||
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', False)
|
||||
@patch('seahub.api2.endpoints.send_share_link_email.IS_EMAIL_CONFIGURED', False)
|
||||
def test_can_send_email_not_configured(self):
|
||||
self.login_as(self.user)
|
||||
invalid_email = 'invalid'
|
||||
url = reverse("api2-send-share-link")
|
||||
data = {
|
||||
"token": self.token,
|
||||
"email": self.admin.email + ',' + invalid_email,
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(403, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 0)
|
||||
|
||||
def test_can_not_send_email_if_not_link_owner(self):
|
||||
self.login_as(self.admin)
|
||||
@ -45,3 +120,4 @@ class SendShareLinkApiTest(BaseTestCase):
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(403, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 0)
|
||||
|
@ -1,9 +1,15 @@
|
||||
#coding: UTF-8
|
||||
import json
|
||||
from mock import patch
|
||||
from django.core import mail
|
||||
from django.core.urlresolvers import reverse
|
||||
from django.test import override_settings
|
||||
|
||||
from seahub.utils import IS_EMAIL_CONFIGURED
|
||||
from seahub.test_utils import BaseTestCase
|
||||
from seahub.share.models import UploadLinkShare
|
||||
from seahub.profile.models import Profile
|
||||
from seahub.profile.utils import refresh_cache
|
||||
from seahub.test_utils import BaseTestCase
|
||||
|
||||
|
||||
class SendUploadLinkApiTest(BaseTestCase):
|
||||
@ -16,7 +22,10 @@ class SendUploadLinkApiTest(BaseTestCase):
|
||||
def tearDown(self):
|
||||
self.remove_repo()
|
||||
|
||||
def test_can_send_email(self):
|
||||
@override_settings(DEFAULT_FROM_EMAIL='from_noreply@seafile.com')
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.IS_EMAIL_CONFIGURED', True)
|
||||
def test_can_send_email_configured(self):
|
||||
self.login_as(self.user)
|
||||
invalid_email = 'invalid'
|
||||
url = reverse("api2-send-upload-link")
|
||||
@ -26,13 +35,75 @@ class SendUploadLinkApiTest(BaseTestCase):
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
if not IS_EMAIL_CONFIGURED:
|
||||
self.assertEqual(403, resp.status_code)
|
||||
else:
|
||||
self.assertEqual(200, resp.status_code)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert json_resp['failed'][0]['email'] == invalid_email
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert json_resp['failed'][0]['email'] == invalid_email
|
||||
assert mail.outbox[0].from_email == 'from_noreply@seafile.com'
|
||||
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.REPLACE_FROM_EMAIL', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.ADD_REPLY_TO_HEADER', True)
|
||||
def test_can_send_email_rewrite(self):
|
||||
self.login_as(self.user)
|
||||
url = reverse("api2-send-upload-link")
|
||||
data = {
|
||||
"token": self.token,
|
||||
"email": self.admin.email,
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert mail.outbox[0].from_email == self.user.email
|
||||
assert mail.outbox[0].extra_headers['Reply-to'] == self.user.email
|
||||
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.IS_EMAIL_CONFIGURED', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.REPLACE_FROM_EMAIL', True)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.ADD_REPLY_TO_HEADER', True)
|
||||
def test_can_send_email_rewrite_contact_email(self):
|
||||
self.login_as(self.user)
|
||||
nickname = 'Testuser'
|
||||
contact_email= 'contact_email@test.com'
|
||||
p = Profile.objects.add_or_update(self.user.email, nickname=nickname)
|
||||
p.contact_email = contact_email
|
||||
p.save()
|
||||
|
||||
refresh_cache(self.user.email)
|
||||
|
||||
url = reverse("api2-send-upload-link")
|
||||
data = {
|
||||
"token": self.token,
|
||||
"email": self.admin.email,
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(200, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 1)
|
||||
json_resp = json.loads(resp.content)
|
||||
assert json_resp['success'][0] == self.admin.email
|
||||
assert mail.outbox[0].from_email == contact_email
|
||||
assert mail.outbox[0].extra_headers['Reply-to'] == contact_email
|
||||
|
||||
@patch('seahub.utils.IS_EMAIL_CONFIGURED', False)
|
||||
@patch('seahub.api2.endpoints.send_upload_link_email.IS_EMAIL_CONFIGURED', False)
|
||||
def test_can_send_email_not_configured(self):
|
||||
self.login_as(self.user)
|
||||
invalid_email = 'invalid'
|
||||
url = reverse("api2-send-upload-link")
|
||||
data = {
|
||||
"token": self.token,
|
||||
"email": self.admin.email + ',' + invalid_email,
|
||||
}
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(403, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 0)
|
||||
|
||||
def test_can_not_send_email_if_not_link_owner(self):
|
||||
self.login_as(self.admin)
|
||||
@ -44,3 +115,4 @@ class SendUploadLinkApiTest(BaseTestCase):
|
||||
|
||||
resp = self.client.post(url, data)
|
||||
self.assertEqual(403, resp.status_code)
|
||||
self.assertEqual(len(mail.outbox), 0)
|
||||
|
Loading…
Reference in New Issue
Block a user