Permalink
Browse files

fixed project api and the unittest

  • Loading branch information...
sweemeng committed Oct 28, 2012
1 parent 31225bb commit 7d14d1702f93cdc05a455547ffd0dbda58001c30
View
@@ -25,7 +25,6 @@ def get(self,project_id):
project = ProjectList()
all_project = []
for p in project.all():
- print p.project.to_mongo()
all_project.append(p.project.to_mongo())
data['all'] = all_project
@@ -55,38 +54,40 @@ def post(self):
def put(self,project_id):
api_key = request.args.get('api_key')
- if not authorized(api_key,project_id):
- return jsonify({'status':False})
-
project = Project()
project.get(project_id)
-
+ user = User()
+ user.api_login(api_key)
data = request.json
-
+
if data.get('action') == 'join':
- user.add_project(project.project.name_to_mongo())
- return jsonify({'status':True})
+ user.add_project(project.get_id())
+ return jsonify({'status':True,'msg':'join project'})
elif data.get('action') == 'withdraw':
- user.remove_project(project.project.name_to_mongo())
- return jsonify({'status':True})
+ user.remove_project(project.get_id())
+ return jsonify({'status':True,'msg':'withdrawn from project'})
- if not project.project.name_to_mongo() in user.user.project:
- return jsonify({'status':False})
+ if not authorized(api_key,project_id):
+ return jsonify({'status':False,'msg':'unauthorized'})
+
+ if not project.get_id() in user.user.project:
+ return jsonify({'status':False,'msg':'project not in user'})
project.project.description = data['description']
project.save()
return jsonify({'status':True})
def delete(self,project_id):
api_key = request.args.get('api_key')
- if not authorized(project_id,api_key):
+ if not authorized(api_key,project_id):
return jsonify({'status':False})
-
+ user = User()
+ user.api_login(api_key)
project = Project()
project.get(project_id)
- if not project.project.name_to_mongo() in user.user.project:
+ if not project.get_id() in user.user.project:
return jsonify({'status':False})
- user.remove_project(project.project.name_to_mongo())
+ user.remove_project(project.get_id())
model = MongoModel(project=project.project_,collection=project.collection_)
model.delete({'_id':objectid.ObjectId(str(project_id))})
@@ -26,13 +26,19 @@ def teardown_test_get():
def setup_user():
user = User()
user.create('test_user','test_pass','test@example.com')
- user.add_project('scraped')
+ project = Project()
+ project.create('scraped','scraped')
+ project.add_entry('entry','entry','localhost')
+ user.add_project(project.get_id())
def teardown_user():
user = User()
user.login('test_user','test_pass')
model = MongoModel(project=user.project,collection=user.collection)
model.delete({'_id':objectid.ObjectId(str(user.user.id))})
+ model = MongoModel(project='internal',collection='project')
+ model.delete({'name':'scraped'})
+
@with_setup(setup_test_get,teardown_test_get)
def test_get_all():
@@ -85,44 +91,53 @@ def test_insert():
mongo.delete(check[0])
def setup_test_update():
- mongo = MongoModel(project='scraped',collection='entry')
+ mongo = MongoModel(project='test_api_update',collection='test_api_entry')
mongo.insert({'a':1})
user = User()
- user.create('test_user','test_pass','test@example.com')
- user.add_project('scraped')
+ user.create('test_user_api_update','test_pass','test@example.com')
+ project = Project()
+ project.create('test api update','scraped')
+ project.add_entry('test api entry','entry','localhost')
+ user.add_project(project.get_id())
def teardown_test_update():
- mongo = MongoModel(project='scraped',collection='entry')
+ mongo = MongoModel(project='test_api_update',collection='test_api_entry')
mongo.delete({'a':2})
user = User()
- user.login('test_user','test_pass')
+ user.login('test_user_api_update','test_pass')
model = MongoModel(project=user.project,collection=user.collection)
model.delete({'_id':objectid.ObjectId(str(user.user.id))})
+ model = MongoModel(project='internal',collection='project')
+ model.delete({'name':'test api update'})
@with_setup(setup_test_update,teardown_test_update)
def test_update():
user = User()
- user.login('test_user','test_pass')
+ user.login('test_user_api_update','test_pass')
api_key = user.user.auth_token
project = Project()
- project.find('scraped')
- entry_id = project.find_entry('entry')
- mongo = MongoModel(project='scraped',collection='entry')
+ project.find('test api update')
+ entry_id = project.find_entry('test api entry')
+ print user.get_project()
+ print project.get_id()
+ mongo = MongoModel(project='test_api_update',collection='test_api_entry')
data = mongo.query({'a':1})
+ print data
id = str(data['_id'])
updated = {'a':2}
url = '/api/db/%s/%s/%s/?api_key=%s' % (project.project.id,entry_id,id,api_key)
client = webapp.app.test_client()
response = client.put(url, data = json.dumps(updated),
content_type='application/json')
-
+ print response.data
status = json.loads(response.data)
-
+
assert status['status']
+
updated_data = mongo.query({'_id':objectid.ObjectId(id)})
assert updated_data['a'] == 2
@@ -103,13 +103,18 @@ def test_project_create():
test_user = User()
test_user.login('test_user','test_pass')
print test_user.user.project
- assert 'project create'.replace(' ','_') in test_user.user.project
+ registered = False
+ for i in project.all():
+ if i.get_id() in test_user.get_project():
+ registered = True
+ assert registered, "project not in user project"
+
exist = False
for i in project.all():
if 'project create' == i.project.name:
exist = True
- assert exist
+ assert exist, "project created"
# now delete it
project = Project()
@@ -118,14 +123,14 @@ def test_project_create():
def setup_project_delete():
user = User()
- user.create('test_user_delete','test_pass','test@example.com')
+ user.create('test_user_delete_api_project','test_pass','test@example.com')
project = Project()
project.create('project 1','project content 1')
- user.add_project('project_1')
+ user.add_project(project.get_id())
def teardown_project_delete():
user = User()
- user.login('test_user_delete','test_pass')
+ user.login('test_user_delete_api_project','test_pass')
project = Project()
model = MongoModel(project=user.project,collection=user.collection)
model.delete({'_id':ObjectId(user.user.id)})
@@ -138,10 +143,13 @@ def teardown_project_delete():
def test_project_delete():
# login user get token
user = User()
- user.login('test_user_delete','test_pass')
+ user.login('test_user_delete_api_project','test_pass')
+
api_key = user.user.auth_token
+ print api_key
project = Project()
project.find('project 1')
+ project_id = project.get_id()
# do a delete
test_client = webapp.app.test_client()
url = '/api/project/%s/?api_key=%s' % (project.project.id,api_key)
@@ -155,24 +163,24 @@ def test_project_delete():
test_user = User()
test_user.login('test_user_delete','test_pass')
- assert 'project_1' not in test_user.user.project
+ assert project_id not in test_user.user.project
def setup_user_project():
# now create user
user = User()
- user.create('test_user_update','test_pass','test@example.com')
+ user.create('test_user_update_project','test_pass','test@example.com')
# create project
project = Project()
project.create('project update','project update content')
# associate project
- user.add_project('project_update')
+ user.add_project(project.get_id())
def teardown_user_project():
user = User()
model = MongoModel(project=user.project,collection=user.collection)
- model.delete({'username':'test_user_update'})
+ model.delete({'username':'test_user_update_project'})
project = Project()
model = MongoModel(project=project.project_,collection=project.collection_)
model.delete({'name':'project update'})
@@ -181,7 +189,7 @@ def teardown_user_project():
@with_setup(setup_user_project,teardown_user_project)
def test_project_update():
user = User()
- user.login('test_user_update','test_pass')
+ user.login('test_user_update_project','test_pass')
api_key = user.user.auth_token
project = Project()
project.find('project update')
@@ -190,6 +198,7 @@ def test_project_update():
data = json.dumps({'description':'project updated content'})
result = test_client.put(url,data=data,content_type='application/json')
status = json.loads(result.data)
+ print status
assert status['status']
project=Project()
@@ -203,13 +212,13 @@ def setup_user_project_list():
user.create('test_user_list','test_pass','test@example.com')
project = Project()
project.create('project list 1','project content 1')
+ user.add_project(project.get_id())
project = Project()
project.create('project list 2','project content 2')
+ user.add_project(project.get_id())
project = Project()
project.create('project list 3','project content 3')
- user.add_project('project_list_1')
- user.add_project('project_list_2')
- user.add_project('project_list_3')
+ user.add_project(project.get_id())
def teardown_user_project_list():
user = User()
@@ -254,26 +263,29 @@ def test_user_project_join():
result = test_client.put(url,data=json.dumps(data),content_type='application/json')
user.login('test_user_list','test_pass')
print user.user.project
- assert 'project_list_3' in user.user.project
+ assert str(project.get_id()) in user.user.project
@with_setup(setup_user_project_list,teardown_user_project_list)
def test_user_project_withdraw():
user = User()
user.login('test_user_list','test_pass')
- user.add_project('project_list_3')
- api_key = user.user.auth_token
- test_client = webapp.app.test_client()
project = Project()
project.find('project list 3')
- url = '/api/project/%s/?api_key=%s' % (project.project.id,api_key)
+ user.add_project(project.get_id())
+ api_key = user.user.auth_token
+ test_client = webapp.app.test_client()
+
+ url = '/api/project/%s/?api_key=%s' % (project.get_id(),api_key)
data = {'project':'project_list_3','action':'withdraw'}
result = test_client.put(url,data=json.dumps(data),content_type='application/json')
print result.data
user.login('test_user_list','test_pass')
print user.user.project
- assert not 'project_list_3' in user.user.project
+ print project.get_id()
+ print project.project.name
+ assert not str(project.get_id()) in user.user.project
@@ -60,6 +60,7 @@ def setup_project_update():
user.create('test_update_user','test_password','test@example.com')
project = Project()
project.create('project update','test project update')
+ user.add_project(project.get_id())
project.add_entry('data','test data','data')
def teardown_project_update():
@@ -82,6 +83,7 @@ def test_project_update():
project_ui = test_client.post('/project/%s/' % project.project.id,data={
'description':'project updated',
},follow_redirects=True)
+ print project_ui.data
assert 'project updated' in project_ui.data
assert 'data' in project_ui.data
View
@@ -25,6 +25,7 @@ def login(self,username,password):
return self
def api_login(self,auth_token):
+ print 'auth_token is %s'%auth_token
temp = self.model.query({'auth_token':auth_token})
if temp:
self.user.from_mongo(temp)
@@ -68,11 +69,12 @@ def get_project(self):
return self.user.project
def add_project(self,project):
- self.user.project.append(project)
+ if project not in self.user.project:
+ self.user.project.append(project)
self.save()
def remove_project(self,project):
- self.user.project.remove(project.replace(' ','_'))
+ self.user.project.remove(project)
self.save()
def update(self,password=None,email=None):
@@ -1,19 +1,29 @@
from user.model import User
from flask.ext.principal import Identity
+from flask.ext.principal import UserNeed
+from flask.ext.principal import identity_loaded
+from flask.ext.principal import identity_changed
+from flask.ext.login import login_user
+from flask import current_app
+from flask import session
from user.permission import EditProjectPermission
from user.permission import edit_project_need
def authorized(api_key,project_id):
user = User()
+ print "api key is %s" % api_key
user.api_login(api_key)
if not user.is_authenticated():
+ print "wrong password"
return False
- identity = Identity(user.get_id())
- for project in user.get_project():
- identity.provides.add(edit_project_need(project))
+ login_user(user)
+ identity_changed.send(current_app._get_current_object(),identity=Identity(user.user.id))
+
permission = EditProjectPermission(project_id)
+
if not permission.can():
+ print "bad permission"
return False
return True

0 comments on commit 7d14d17

Please sign in to comment.