Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Finished server code

  • Loading branch information...
commit 7e876981b5eb486f95e3891ccc2797126c9b0e92 1 parent e474d2e
@sschaetz authored
View
BIN  doc/server_data_structure.sql
Binary file not shown
View
1  doc/server_data_structure.sql
View
BIN  server/default.sql
Binary file not shown
View
90 server/server.py
@@ -2,6 +2,7 @@
import ConfigParser
import sqlite3
import json
+import time
from werkzeug.wrappers import Response, Request
from werkzeug.routing import Map, Rule
@@ -41,7 +42,7 @@ def __init__(self):
Rule('/<user>/load_data/<authkey>', endpoint='load_data'),
Rule('/<user>/store_data', endpoint='store_data'),
Rule('/<user>/load_messages', endpoint='load_messages'),
- Rule('/<user>/inbox/<accesstoken>', endpoint='inbox'),
+ Rule('/<user>/inbox', endpoint='inbox'),
Rule('/<user>/add_accesstokens', endpoint='add_accesstokens'),
Rule('/<user>/remove_accesstokens', endpoint='remove_accesstokens'),
Rule('/createuser', endpoint='createuser'),
@@ -81,13 +82,13 @@ def __call__(self, environ, start_response):
# here are the responders -----
def on_root(self, request):
- return Response("on_root")
+ return Response("This is starbase!")
def on_default(self, request, user):
- return Response("on_default " + user)
+ return Response("This is the starbase home of " + user + ".")
def on_load_data(self, request, user, authkey):
- self.user_authenticate(authkey)
+ self.authenticate_user(authkey)
return data_response(self.get_data())
def on_store_data(self, request, user):
@@ -96,24 +97,43 @@ def on_store_data(self, request, user):
if not 'data' in request.form:
raise BadRequest()
- self.user_authenticate(request.form['authkey'])
+ self.authenticate_user(request.form['authkey'])
self.set_data(request.form['data'])
return success_response()
def on_load_messages(self, request, user):
if not 'authkey' in request.form:
raise Unauthorized()
- self.user_authenticate(request.form['authkey'])
- return Response("load_messages")
+ self.authenticate_user(request.form['authkey'])
+ return data_response(self.get_messages())
- def on_inbox(self, request, user, accesstoken):
- return Response("inbox " + user + " " + accesstoken)
+ def on_inbox(self, request, user):
+ if not 'accesstoken' in request.form:
+ raise Unauthorized()
+ if not 'message' in request.form:
+ raise BadRequest()
+ self.authenticate_friend(request.form['accesstoken'])
+ self.store_message(request.form['message'], request.form['accesstoken'])
+ return success_response()
def on_add_accesstokens(self, request, user):
- return Response("add_accesstokens")
+ if not 'authkey' in request.form:
+ raise Unauthorized()
+ self.authenticate_user(request.form['authkey'])
+ if not 'accesstokens' in request.form:
+ print "no access tokens"
+ raise BadRequest()
+ self.insert_accesstokens(request.form['accesstokens'])
+ return success_response()
def on_remove_accesstokens(self, request, user):
- return Response("remove_accesstokens")
+ if not 'authkey' in request.form:
+ raise Unauthorized()
+ self.authenticate_user(request.form['authkey'])
+ if not 'accesstokens' in request.form:
+ raise BadRequest()
+ self.delete_accesstokens(request.form['accesstokens'])
+ return success_response()
# create a new user
def on_createuser(self, request):
@@ -138,14 +158,29 @@ def user_exists(self, user):
# database functions -----
- def user_authenticate(self, authkey):
+ def authenticate_user(self, authkey):
c = self.db.cursor()
c.execute('SELECT 1 FROM admin WHERE authkey = ?', [authkey])
rows = len(c.fetchall())
c.close()
if rows != 1:
raise Unauthorized()
-
+
+ def authenticate_friend(self, accesstoken):
+ c = self.db.cursor()
+ c.execute('SELECT 1 FROM friends WHERE accesstoken = ?', [accesstoken])
+ rows = len(c.fetchall())
+ c.close()
+ if rows < 1:
+ raise Unauthorized()
+
+ def store_message(self, message, accesstoken):
+ c = self.db.cursor()
+ c.execute("INSERT INTO inbox VALUES (?, ?, ?)",
+ [message, accesstoken, time.time()])
+ self.db.commit()
+ c.close()
+
def get_data(self):
c = self.db.cursor()
c.execute('SELECT data FROM blobs WHERE name = ?', ["datablob"])
@@ -159,6 +194,35 @@ def set_data(self, data):
self.db.commit()
return
+ def insert_accesstokens(self, accesstokens):
+ try:
+ c = self.db.cursor()
+ for token in json.loads(accesstokens):
+ c.execute('INSERT INTO friends VALUES (?)', [token])
+ self.db.commit()
+ c.close()
+ except:
+ raise BadRequest("accesstokens could not be inserted")
+
+ def delete_accesstokens(self, accesstokens):
+ try:
+ c = self.db.cursor()
+ for token in json.loads(accesstokens):
+ c.execute('DELETE FROM friends WHERE accesstoken = ?', [token])
+ self.db.commit()
+ c.close()
+ except:
+ raise BadRequest("accesstokens could not be deleted")
+
+ def get_messages(self):
+ c = self.db.cursor()
+ c.execute('SELECT * FROM inbox')
+ data = c.fetchall()
+ c.execute("DELETE FROM inbox")
+ self.db.commit()
+ c.close()
+ return data
+
def create_app():
app = starbase()
View
65 server/unit/test.py
@@ -32,7 +32,7 @@ def setUp(self):
self.user_data_folder = self.config.get('general', 'user_data_folder')
self.domain = self.config.get('general', 'domain')
- def test_create_users(self):
+ def test_001_create_users(self):
url = self.domain + "createuser"
result = fire_request_json(url, {'user': 'seb', 'authkey': 'sebauthkey'})
self.assertEqual(result, "OK")
@@ -43,7 +43,7 @@ def test_create_users(self):
self.assertRaises(urllib2.HTTPError, fire_request_json,
url, {'user': 'clau', 'authkey': 'clauauthkey'})
- def test_store_data(self):
+ def test_002_store_data(self):
# store data
result = fire_request_json(self.domain + "seb/store_data",
{'authkey': 'sebauthkey', 'data': 'mydata'})
@@ -52,6 +52,67 @@ def test_store_data(self):
result = fire_request_json(self.domain + "seb/load_data/sebauthkey")
self.assertEqual(result, "mydata")
+ def test_003_add_accesstokens(self):
+ # add 1 accesstoken
+ result = fire_request_json(self.domain + "seb/add_accesstokens",
+ {'authkey': 'sebauthkey', 'accesstokens': json.dumps(["token1"])})
+ self.assertEqual(result, "OK")
+ # add multiple accesstokens
+ result = fire_request_json(self.domain + "seb/add_accesstokens",
+ {
+ 'authkey': 'sebauthkey',
+ 'accesstokens': json.dumps(["token2", "token3"])
+ })
+ self.assertEqual(result, "OK")
+ # add token for testing purposes
+ result = fire_request_json(self.domain + "seb/add_accesstokens",
+ {'authkey': 'sebauthkey', 'accesstokens': json.dumps(["token4"])})
+ self.assertEqual(result, "OK")
+
+ def test_004_remove_accesstokens(self):
+ # remove 1 accesstoken
+ result = fire_request_json(self.domain + "seb/remove_accesstokens",
+ {'authkey': 'sebauthkey', 'accesstokens': json.dumps(["token1"])})
+ self.assertEqual(result, "OK")
+ # remove multiple accesstokens
+ result = fire_request_json(self.domain + "seb/remove_accesstokens",
+ {
+ 'authkey': 'sebauthkey',
+ 'accesstokens': json.dumps(["token2", "token3"])
+ })
+ self.assertEqual(result, "OK")
+
+ def test_005_send_message(self):
+ # send message
+ result = fire_request_json(self.domain + "seb/inbox",
+ {'accesstoken': 'token4', 'message': 'HI!'})
+ self.assertEqual(result, "OK")
+
+ def test_006_load_messages(self):
+ # get messages
+ result = fire_request_json(self.domain + "seb/load_messages",
+ {'authkey': 'sebauthkey'})
+ self.assertEqual(result[0][0], "HI!")
+ self.assertEqual(result[0][1], "token4")
+
+ def test_007_send_messages(self):
+ # send messages
+ result = fire_request_json(self.domain + "seb/inbox",
+ {'accesstoken': 'token4', 'message': 'message2'})
+ self.assertEqual(result, "OK")
+ result = fire_request_json(self.domain + "seb/inbox",
+ {'accesstoken': 'token4', 'message': 'message3'})
+ self.assertEqual(result, "OK")
+
+ def test_008_load_messages(self):
+ # get messages
+ result = fire_request_json(self.domain + "seb/load_messages",
+ {'authkey': 'sebauthkey'})
+ self.assertEqual(result[0][0], "message2")
+ self.assertEqual(result[0][1], "token4")
+ self.assertEqual(result[1][0], "message3")
+ self.assertEqual(result[1][1], "token4")
+
# clear create or clear folder
def setup():
# get configuration information
Please sign in to comment.
Something went wrong with that request. Please try again.