-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathtestFAPI.py
More file actions
153 lines (121 loc) · 5.56 KB
/
testFAPI.py
File metadata and controls
153 lines (121 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" Copyright 2012-2025 Smartling, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this work except in compliance with the License.
* You may obtain a copy of the License in the LICENSE file, or at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
"""
import os
import sys
import time
isPython3 = sys.version_info[:2] >= (3,0)
if isPython3:
newline = b"\n"
else:
newline = "\n"
lib_path = os.path.abspath('../')
sys.path.append(lib_path) # allow to import ../smartlingApiSdk/SmartlingFileApi
import smartlingApiSdk
from smartlingApiSdk.ObsoleteSmartlingFileApi import ObsoleteSmartlingFileApi, SmartlingFileApiFactory
from smartlingApiSdk.ProxySettings import ProxySettings
from smartlingApiSdk.UploadData import UploadData
from nose.tools import assert_equal
noKeymessage = """ don't forget to set real API_KEY and PROJECT_ID
or use environment variables:
export SL_API_KEY=********-****-****-****-************
export SL_PROJECT_ID=*******
"""
class testFapi(object):
MY_API_KEY = "YOUR_API_KEY"
MY_PROJECT_ID = "YOUR_PROJECT_ID"
FILE_NAME = "java.properties"
FILE_NAME_16 = "javaUTF16.properties"
FILE_TYPE = "javaProperties"
FILE_PATH = smartlingApiSdk.__path__[0]+"/resources/"
FILE_NAME_NEW = "java.properties.renamed"
FILE_NAME_NEW_16 = "javaUTF16.properties.renamed"
CALLBACK_URL = "http://google.com/?q=hello"
SUCCESS_TOKEN = 'SUCCESS'
def setUp(self):
self.MY_API_KEY = os.environ.get('SL_API_KEY', self.MY_API_KEY)
if self.MY_API_KEY == "YOUR_API_KEY":
raise Exception(noKeymessage)
self.MY_PROJECT_ID = os.environ.get('SL_PROJECT_ID', self.MY_PROJECT_ID)
if self.MY_PROJECT_ID == "YOUR_PROJECT_ID":
raise Exception(noKeymessage)
useProxy = False
if useProxy :
proxySettings = ProxySettings("login", "password", "proxy_host", "proxy_port or None")
else:
proxySettings = None
self.fapi = SmartlingFileApiFactory().getSmartlingTranslationApi(self.MY_API_KEY, self.MY_PROJECT_ID, proxySettings)
self.locale = os.environ.get('SL_LOCALE', "ru-RU")
timestamp = repr(time.time())
self.uri = self.FILE_NAME + timestamp
self.doUpload(self.FILE_NAME, self.uri)
self.uri16 = self.FILE_NAME_16 + timestamp
self.doUpload(self.FILE_NAME_16, self.uri16)
def tearDown(self):
time.sleep(10)
res, status = self.fapi.delete(self.uri)
res, status = self.fapi.delete(self.uri16)
def doUpload(self, name, uri):
#ensure file is uploaded which is necessary for all tests
uploadData = UploadData(self.FILE_PATH, name, self.FILE_TYPE)
uploadData.setUri(uri)
uploadData.setCallbackUrl(self.CALLBACK_URL)
return self.fapi.upload(uploadData)
def testFileList(self):
res, status = self.fapi.list()
assert_equal(self.SUCCESS_TOKEN, res.code)
def testFileStatus(self):
res, status = self.fapi.status(self.uri, self.locale)
if str(status) == '423':
assert_equal('RESOURCE_LOCKED', res.code)
else:
assert_equal(self.SUCCESS_TOKEN, res.code)
res, status = self.fapi.status(self.uri16, self.locale)
if str(status) == '423':
assert_equal('RESOURCE_LOCKED', res.code)
else:
assert_equal(self.SUCCESS_TOKEN, res.code)
def testGetFileFromServer(self):
res, status = self.fapi.get(self.uri, self.locale)
lines = open(self.FILE_PATH + self.FILE_NAME, "rb").read().split(newline)
assert_equal(len(res.split(newline)), len(lines))
res, status = self.fapi.get(self.uri16, self.locale)
lines = open(self.FILE_PATH + self.FILE_NAME_16, "rb").read().split(newline)
assert_equal(len(res.split(newline)), len(lines))
def testGetFileWithTypeFromServer(self):
res, status = self.fapi.get(self.uri, self.locale, retrievalType='pseudo')
lines = open(self.FILE_PATH + self.FILE_NAME, "rb").read().split(newline)
assert_equal(len(res.split(newline)), len(lines))
res, status = self.fapi.get(self.uri16, self.locale, retrievalType='pseudo')
lines = open(self.FILE_PATH + self.FILE_NAME_16, "rb").read().split(newline)
assert_equal(len(res.split(newline)), len(lines))
def testFileDelete(self):
res, status = self.fapi.delete(self.uri)
assert_equal(self.SUCCESS_TOKEN, res.code)
res, status = self.fapi.delete(self.uri16)
assert_equal(self.SUCCESS_TOKEN, res.code)
def testFileRename(self):
res, status = self.fapi.rename(self.uri, self.FILE_NAME_NEW)
assert_equal(self.SUCCESS_TOKEN, res.code)
res, status = self.fapi.rename(self.FILE_NAME_NEW, self.uri)
assert_equal(self.SUCCESS_TOKEN, res.code)
def testLastModified(self):
res, status = self.fapi.last_modified(self.uri)
assert_equal(self.SUCCESS_TOKEN, res.code)
assert_equal(True, len(res.data.items)>0)
res, status = self.fapi.last_modified(self.uri16)
assert_equal(self.SUCCESS_TOKEN, res.code)
assert_equal(True, len(res.data.items)>0)