88import io
99
1010import six
11+
12+ if six .PY2 :
13+ from urlparse import urlparse
14+ if six .PY3 :
15+ from urllib .parse import urlparse
16+
1117import requests
1218from nose .tools import with_setup # type: ignore
1319from nose .tools import assert_raises # type: ignore
@@ -24,6 +30,10 @@ def setup_func():
2430 leancloud .init (os .environ ["APP_ID" ], master_key = os .environ ["MASTER_KEY" ])
2531
2632
33+ def setup_without_master_key ():
34+ leancloud .init (os .environ ["APP_ID" ], os .environ ["APP_KEY" ])
35+
36+
2737def test_basic (): # type: () -> None
2838 def fn (s ):
2939 f = File ("Blah" , s , mime_type = "text/plain" )
@@ -97,6 +107,34 @@ def test_save(): # type: () -> None
97107 assert not f .url .endswith ("." )
98108
99109
110+ @with_setup (setup_func )
111+ def test_save_with_specified_key (): # type: () -> None
112+ f = File ("Blah.txt" , open ("tests/sample_text.txt" , "rb" ))
113+ user_specified_key = "abc"
114+ f .key = user_specified_key
115+ f .save ()
116+
117+ assert f .id
118+ assert f .name == "Blah.txt"
119+ assert f .mime_type == "text/plain"
120+ path = urlparse (f .url ).path
121+ if path .startswith ("/avos-cloud-" ): # old school aws s3 file url
122+ assert path .split ("/" )[2 ] == user_specified_key
123+ else :
124+ assert path == "/" + user_specified_key
125+
126+
127+ @with_setup (setup_without_master_key )
128+ def test_save_with_specified_key_but_without_master_key (): # type: () -> None
129+ f = File ("Blah.txt" , open ("tests/sample_text.txt" , "rb" ))
130+ f .key = "abc"
131+ try :
132+ f .save ()
133+ except LeanCloudError as e :
134+ if e .code == 1 and e .error .startswith ("Unsupported file key" ):
135+ pass
136+
137+
100138@with_setup (setup_func )
101139def test_query (): # type: () -> None
102140 files = leancloud .Query ("File" ).find ()
0 commit comments