-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrestful_api_test.py
More file actions
79 lines (69 loc) · 2.45 KB
/
restful_api_test.py
File metadata and controls
79 lines (69 loc) · 2.45 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
from urllib import urlencode
from httplib2 import Http
import json
import sys
import base64
print "Running Endpoint Tester....\n"
address = raw_input("Please enter the address of the server you want to access, \n If left blank the connection will be set to 'http://localhost:5000': ")
if address == '':
address = 'http://localhost:5000'
#TEST 1 TRY TO MAKE A NEW USER
try:
url = address + '/users'
h = Http()
data = dict(username = "TinnyTim", password = "Udacity")
data = json.dumps(data)
resp, content = h.request(url,'POST', body = data, headers = {"Content-Type": "application/json"})
if resp['status'] != '201' and resp['status'] != '200':
raise Exception('Received an unsuccessful status code of %s' % resp['status'])
except Exception as err:
print "Test 1 FAILED: Could not make a new user"
print err.args
sys.exit()
else:
print "Test 1 PASS: Succesfully made a new user"
#TEST 2 ADD NEW RESTAURANT TO THE DATABASE
try:
h = Http()
h.add_credentials('TinnyTim','Udacity')
url = address + '/restaurants'
data = dict(username = "TinnyTim", password = "Udacity", location = "goiania goias", mealtype = "pizza")
resp, content = h.request(url,'POST', body = json.dumps(data), headers = {"Content-Type" : "application/json"})
if resp['status'] != '200':
raise Exception('Received an unsuccessful status code of %s' % resp['status'])
except Exception as err:
print "Test 2 FAILED: Could not add new restaurant"
print err.args
sys.exit()
else:
print "Test 2 PASS: Succesfully add a restaurant"
#TEST 3 TRY TO READ RESTAURANTS WITH INVALID CREDENTIALS
try:
h = Http()
h.add_credentials('TinnyTim','Youdacity')
url = address + '/restaurants'
data = dict(username = "Tinny_Tim", password = "youdacity")
resp, content = h.request(url,'GET', urlencode(data))
if resp['status'] == '200':
raise Exception("Security Flaw: able to log in with invalid credentials")
except Exception as err:
print "Test 3 FAILED"
print err.args
sys.exit()
else:
print "Test 3 PASS: App checks against invalid credentials"
#TEST 4 TRY TO READ RESTAURANTS WITH VALID CREDENTIALS
try:
h = Http()
h.add_credentials("TinnyTim", "Udacity")
url = address + '/restaurants'
resp, content = h.request(url,'GET')
if resp['status'] != '200':
raise Exception("Unable to access /restaurants with valid credentials")
except Exception as err:
print "Test 4 FAILED"
print err.args
sys.exit()
else:
print "Test 4 PASS: Logged in User can view /restaurants"
print "ALL TESTS PASSED!"