Skip to content

Commit ebad8c9

Browse files
authored
Create a new Clarifai app per test run
* Create a new app per test run * Remove testing tokens * Update the tested nodejs versions * Update app manipulation script
1 parent 81453e9 commit ebad8c9

File tree

5 files changed

+194
-98
lines changed

5 files changed

+194
-98
lines changed

.travis.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
language: node_js
2+
23
node_js:
3-
- '6'
44
- '8'
5+
- '10'
56
- 'node'
7+
68
branches:
79
only:
810
- master
911
- /^\d+\.\d+\.\d+$/
12+
1013
cache:
1114
directories:
1215
- node_modules
16+
1317
before_script:
14-
- chmod +x token.sh
18+
- export PYTHONPATH=.
19+
- export CLARIFAI_APP_ID="$(python scripts/app_and_key_for_tests.py --create-app javascript-travis)"
20+
- export CLARIFAI_API_KEY="$(python scripts/app_and_key_for_tests.py --create-key ${CLARIFAI_APP_ID})"
21+
1522
script:
16-
- source token.sh
1723
- npm run test
1824
- npm run build
25+
26+
after_script:
27+
- export PYTHONPATH=.
28+
- python scripts/app_and_key_for_tests.py --delete-app $CLARIFAI_APP_ID
29+
1930
deploy:
2031
- provider: s3
2132
access_key_id: ${AWS_ACCESS_KEY_ID}

scripts/app_and_key_for_tests.py

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
import json
2+
import os
3+
import sys
4+
5+
try:
6+
from urllib.parse import urlparse, urlencode
7+
from urllib.request import urlopen, Request, build_opener, HTTPHandler
8+
from urllib.error import HTTPError
9+
except ImportError:
10+
from urlparse import urlparse
11+
from urllib import urlencode
12+
from urllib2 import urlopen, Request, HTTPError, build_opener, HTTPHandler
13+
14+
15+
EMAIL = os.environ['CLARIFAI_USER_EMAIL']
16+
PASSWORD = os.environ['CLARIFAI_USER_PASSWORD']
17+
18+
19+
BASE = 'https://api.clarifai.com/v2'
20+
21+
22+
def _request(method, url, payload={}, headers={}):
23+
opener = build_opener(HTTPHandler)
24+
full_url = '%s%s' % (BASE, url)
25+
request = Request(full_url, data=json.dumps(payload).encode())
26+
for k in headers.keys():
27+
request.add_header(k, headers[k])
28+
request.get_method = lambda: method
29+
return json.loads(opener.open(request).read().decode())
30+
31+
32+
def create_app(env_name):
33+
session_token, user_id = _login()
34+
35+
url = '/users/%s/apps' % user_id
36+
payload = {'apps': [{'name': 'auto-created-in-%s-ci-test-run' % env_name}]}
37+
38+
response = _request(method='POST', url=url, payload=payload, headers=_auth_headers(session_token))
39+
40+
_raise_on_http_error(response)
41+
data = response
42+
app_id = data['apps'][0]['id']
43+
44+
# This print needs to be present so we can read the value in CI.
45+
print(app_id)
46+
47+
48+
def create_key(app_id):
49+
session_token, user_id = _login()
50+
51+
url = '/users/%s/keys' % user_id
52+
payload = {
53+
'keys': [{
54+
'description': 'Auto-created in a CI test run',
55+
'scopes': ['All'],
56+
'apps': [{'id': app_id, 'user_id': user_id}]
57+
}]
58+
}
59+
response = _request(method='POST', url=url, payload=payload, headers=_auth_headers(session_token))
60+
_raise_on_http_error(response)
61+
data = response
62+
key_id = data['keys'][0]['id']
63+
64+
# This print needs to be present so we can read the value in CI.
65+
print(key_id)
66+
67+
68+
def delete(app_id):
69+
session_token, user_id = _login()
70+
71+
# All the related keys will be deleted automatically when the app is deleted
72+
_delete_app(session_token, user_id, app_id)
73+
74+
75+
def create_sample_workflow(api_key):
76+
url = '/workflows'
77+
payload = {
78+
'workflows': [
79+
{
80+
'id': 'food-and-general',
81+
'nodes': [
82+
{
83+
'id': 'food-workflow-node',
84+
'model': {
85+
'id': 'bd367be194cf45149e75f01d59f77ba7',
86+
'model_version': {
87+
'id': 'dfebc169854e429086aceb8368662641'
88+
}
89+
}
90+
},
91+
{
92+
'id': 'general-workflow-node',
93+
'model': {
94+
'id': 'aaa03c23b3724a16a56b629203edc62c',
95+
'model_version': {
96+
'id': 'aa9ca48295b37401f8af92ad1af0d91d'
97+
}
98+
}
99+
}
100+
]
101+
}
102+
]
103+
}
104+
response = _request(method='POST', url=url, payload=payload, headers=_auth_headers_for_api_key_key(api_key))
105+
_raise_on_http_error(response)
106+
107+
108+
def _delete_app(session_token, user_id, app_id):
109+
url = '/users/%s/apps/%s' % (user_id, app_id)
110+
response = _request(method='DELETE', url=url, headers=_auth_headers(session_token))
111+
_raise_on_http_error(response)
112+
113+
114+
def _auth_headers(session_token):
115+
headers = {'Content-Type': 'application/json', 'X-Clarifai-Session-Token': session_token}
116+
return headers
117+
118+
119+
def _auth_headers_for_api_key_key(api_key):
120+
headers = {'Content-Type': 'application/json', 'Authorization': 'Key ' + api_key}
121+
return headers
122+
123+
124+
def _login():
125+
url = '/login'
126+
payload = {'email': EMAIL, 'password': PASSWORD}
127+
response = _request(method='POST', url=url, payload=payload)
128+
_raise_on_http_error(response)
129+
data = response
130+
user_id = data['v2_user_id']
131+
session_token = data['session_token']
132+
return session_token, user_id
133+
134+
135+
def _raise_on_http_error(response):
136+
# TODO: Make this work with urllib.
137+
# if int(response.status_code) // 100 != 2:
138+
# raise Exception('Unexpected response %s: %s' % (response.status_code, response.text))
139+
pass
140+
141+
142+
def run(arguments):
143+
command = arguments[0] if arguments else '--help'
144+
if command == '--create-app':
145+
if len(arguments) != 2:
146+
raise Exception('--create-app takes one argument')
147+
148+
env_name = arguments[1]
149+
create_app(env_name)
150+
elif command == '--create-key':
151+
if len(arguments) != 2:
152+
raise Exception('--create-key takes one argument')
153+
154+
app_id = arguments[1]
155+
create_key(app_id)
156+
elif command == '--delete-app':
157+
if len(arguments) != 2:
158+
raise Exception('--delete-app takes one argument')
159+
app_id = arguments[1]
160+
delete(app_id)
161+
elif command == '--create-workflow':
162+
if len(arguments) != 2:
163+
raise Exception('--create-workflow takes one argument')
164+
api_key = arguments[1]
165+
create_sample_workflow(api_key)
166+
elif command == '--help':
167+
print('''DESCRIPTION: Creates and delete applications and API keys
168+
ARGUMENTS:
169+
--create-app [env_name] ... Creates a new application.
170+
--create-key [app_id] ... Creates a new API key.
171+
--delete-app [app_id] ... Deletes an application (API keys that use it are deleted as well).
172+
--create-workflow [api_key] ... Creates a sample workflow to be used in int. tests.
173+
--help ... This text.''')
174+
else:
175+
print('Unknown argument. Please see --help')
176+
exit(1)
177+
178+
179+
if __name__ == '__main__':
180+
run(arguments=sys.argv[1:])

tests/integration/api-key-int-tests.js

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,4 @@ describe('Integration Tests - API key', () => {
3535
})
3636
.catch(errorHandler.bind(done));
3737
});
38-
39-
it('Sets a token with an object', done => {
40-
const token = {
41-
access_token: 'foo',
42-
token_type: 'Bearer',
43-
expires_in: 100000,
44-
scope: 'api_access_write api_access api_access_read'
45-
};
46-
const anApp = new Clarifai.App(null, null, {token: token});
47-
anApp._config.token()
48-
.then(response => {
49-
expect(response.accessToken).toEqual('foo');
50-
done();
51-
})
52-
.catch(errorHandler.bind(done));
53-
});
54-
55-
it('Sets a token with a string', done => {
56-
const anApp = new Clarifai.App(null, null, {token: 'bar'});
57-
anApp._config.token()
58-
.then(response => {
59-
expect(response.accessToken).toEqual('bar');
60-
done();
61-
})
62-
.catch(errorHandler.bind(done));
63-
});
6438
});

tests/integration/session-token-int-tests.js

Lines changed: 0 additions & 66 deletions
This file was deleted.

token.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)