api_tests.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # Licensed to Cloudera, Inc. under one
  4. # or more contributor license agreements. See the NOTICE file
  5. # distributed with this work for additional information
  6. # regarding copyright ownership. Cloudera, Inc. licenses this file
  7. # to you under the Apache License, Version 2.0 (the
  8. # "License"); you may not use this file except in compliance
  9. # with the License. You may obtain a copy of the License at
  10. #
  11. # http://www.apache.org/licenses/LICENSE-2.0
  12. #
  13. # Unless required by applicable law or agreed to in writing, software
  14. # distributed under the License is distributed on an "AS IS" BASIS,
  15. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. # See the License for the specific language governing permissions and
  17. # limitations under the License.
  18. import json
  19. from nose.tools import assert_true, assert_false, assert_equal, assert_not_equal, assert_raises
  20. from django.conf.urls.defaults import patterns, url
  21. from django.contrib.auth.models import User
  22. from django.core.urlresolvers import reverse
  23. from django.http import HttpResponse
  24. from django.db.models import query, CharField, SmallIntegerField
  25. from desktop.lib.django_test_util import make_logged_in_client
  26. from desktop.lib.test_utils import grant_access
  27. from desktop.models import DocumentTag , Document
  28. from pig.models import PigScript
  29. from useradmin.models import get_default_user_group
  30. from desktop.api import massaged_documents_for_json, _get_docs
  31. class TestDocModelTags():
  32. def setUp(self):
  33. self.client = make_logged_in_client(username="tag_user", recreate=True, is_superuser=False)
  34. self.client_not_me = make_logged_in_client(username="not_tag_user", recreate=True, is_superuser=False)
  35. self.user = User.objects.get(username="tag_user")
  36. self.user_not_me = User.objects.get(username="not_tag_user")
  37. grant_access(self.user.username, self.user.username, "desktop")
  38. grant_access(self.user_not_me.username, self.user_not_me.username, "desktop")
  39. def add_tag(self, name):
  40. response = self.client.post("/desktop/api/tag/add_tag", {'name': name})
  41. assert_equal(0, json.loads(response.content)['status'], response.content)
  42. return json.loads(response.content)['id']
  43. def add_doc(self, name):
  44. script = PigScript.objects.create(owner=self.user)
  45. doc = Document.objects.link(script, owner=script.owner, name=name)
  46. return script, doc
  47. def test_add_tag(self):
  48. response = self.client.get("/desktop/api/tag/add_tag")
  49. assert_equal(-1, json.loads(response.content)['status'])
  50. tag_id = self.add_tag('my_tag')
  51. assert_true(DocumentTag.objects.filter(id=tag_id, owner=self.user, tag='my_tag').exists())
  52. def test_add_duplicate_tag(self):
  53. tag_name = 'test_add_duplicate_tag'
  54. n = DocumentTag.objects.filter(owner=self.user, tag=tag_name).count()
  55. tag_id = self.add_tag(tag_name)
  56. assert_equal(n + 1, DocumentTag.objects.filter(owner=self.user, tag=tag_name).count())
  57. tag_id = self.add_tag(tag_name)
  58. assert_equal(n + 1, DocumentTag.objects.filter(owner=self.user, tag=tag_name).count())
  59. def test_add_and_clean_duplicate_tag(self):
  60. tag_name = 'test_add_and_clean_duplicate_tag'
  61. script, doc = self.add_doc('test-pig')
  62. n = DocumentTag.objects.filter(owner=self.user, tag=tag_name).count()
  63. tag_id = self.add_tag(tag_name)
  64. assert_equal(n + 1, DocumentTag.objects.filter(owner=self.user, tag=tag_name).count())
  65. DocumentTag.objects.create(owner=self.user, tag=tag_name)
  66. assert_equal(n + 2, DocumentTag.objects.filter(owner=self.user, tag=tag_name).count())
  67. tag_id = DocumentTag.objects.tag(self.user, doc.id, tag_name=tag_name)
  68. assert_equal(n + 1, DocumentTag.objects.filter(owner=self.user, tag=tag_name).count())
  69. def test_remove_tags(self):
  70. response = self.client.post("/desktop/api/tag/add_tag", {'name': 'my_tag'})
  71. tag_id = json.loads(response.content)['id']
  72. response = self.client.get("/desktop/api/tag/remove_tag")
  73. assert_equal(-1, json.loads(response.content)['status'])
  74. response = self.client_not_me.post("/desktop/api/tag/remove_tag", {'tag_id': tag_id})
  75. assert_equal(-1, json.loads(response.content)['status'], response.content)
  76. response = self.client.post("/desktop/api/tag/remove_tag", {'tag_id': tag_id})
  77. assert_equal(0, json.loads(response.content)['status'], response.content)
  78. assert_false(DocumentTag.objects.filter(id=tag_id).exists())
  79. def test_massaged_documents_for_json(self):
  80. docs = _get_docs(self.user)
  81. assert_equal({}, massaged_documents_for_json(docs, self.user))
  82. tag_name = 'test_massaged_documents_for_json'
  83. script, doc = self.add_doc('test_massaged_documents_for_json')
  84. docs = _get_docs(self.user)
  85. assert_not_equal({}, massaged_documents_for_json(docs, self.user))
  86. def test_tag(self):
  87. script, doc = self.add_doc('tag_pig')
  88. response = self.client.post("/desktop/api/doc/tag", {'data': json.dumps({'doc_id': doc.id, 'tag': 'pig'})})
  89. assert_equal(0, json.loads(response.content)['status'], response.content)
  90. tag2_id = self.add_tag('pig2')
  91. response = self.client.post("/desktop/api/doc/tag", {'data': json.dumps({'doc_id': doc.id, 'tag_id': tag2_id})})
  92. assert_equal(0, json.loads(response.content)['status'], response.content)
  93. def test_update_tags(self):
  94. script, doc = self.add_doc('update_tags')
  95. default_tag = DocumentTag.objects.get_default_tag(self.user)
  96. tag1_id = self.add_tag('update_tags_1')
  97. tag2_id = self.add_tag('update_tags_2')
  98. response = self.client.post("/desktop/api/doc/update_tags", {'data': json.dumps({'doc_id': doc.id, 'tag_ids': [tag1_id, tag2_id]})})
  99. content = json.loads(response.content)
  100. assert_equal(0, content['status'], content)
  101. assert_equal([
  102. {"id": default_tag.id, "name": "default"},
  103. {"id": tag1_id, "name": "update_tags_1"},
  104. {"id": tag2_id, "name": "update_tags_2"}
  105. ], content['doc']['tags'])
  106. # No perms
  107. response = self.client_not_me.post("/desktop/api/doc/update_tags", {'data': json.dumps({'doc_id': doc.id, 'tag_ids': [tag1_id, tag2_id]})})
  108. content = json.loads(response.content)
  109. assert_equal(-1, content['status'])
  110. # todo no default tag on test user?
  111. class TestDocModelPermissions():
  112. def setUp(self):
  113. self.client = make_logged_in_client(username="perm_user", groupname="default", recreate=True, is_superuser=False)
  114. self.client_not_me = make_logged_in_client(username="not_perm_user", groupname="default", recreate=True, is_superuser=False)
  115. self.user = User.objects.get(username="perm_user")
  116. self.user_not_me = User.objects.get(username="not_perm_user")
  117. grant_access(self.user.username, self.user.username, "desktop")
  118. grant_access(self.user_not_me.username, self.user_not_me.username, "desktop")
  119. PigScript.objects.filter(owner=self.user).delete()
  120. Document.objects.filter(owner=self.user).delete()
  121. def _add_doc(self, name):
  122. script, created = PigScript.objects.get_or_create(owner=self.user)
  123. doc = Document.objects.link(script, owner=script.owner, name=name)
  124. return script, doc
  125. def test_update_permissions(self):
  126. script, doc = self._add_doc('test_update_permissions')
  127. response = self.client.post("/desktop/api/doc/update_permissions", {
  128. 'doc_id': doc.id,
  129. 'data': json.dumps({'read': {'user_ids': [self.user.id, self.user_not_me.id], 'group_ids': []}})
  130. })
  131. assert_equal(0, json.loads(response.content)['status'], response.content)
  132. def test_share_document_permissions(self):
  133. # No doc
  134. response = self.client.get('/home')
  135. assert_equal({}, json.loads(response.context['json_documents']))
  136. response = self.client_not_me.get('/home')
  137. assert_equal({}, json.loads(response.context['json_documents']))
  138. # Add doc
  139. script, doc = self._add_doc('test_update_permissions')
  140. doc_id = '%s' % doc.id
  141. response = self.client.get('/home')
  142. assert_true(doc_id in json.loads(response.context['json_documents']))
  143. response = self.client_not_me.get('/home')
  144. assert_false(doc_id in json.loads(response.context['json_documents']))
  145. # Share by user
  146. response = self.client.post("/desktop/api/doc/update_permissions", {
  147. 'doc_id': doc.id,
  148. 'data': json.dumps({
  149. 'read': {
  150. 'user_ids': [
  151. self.user.id,
  152. self.user_not_me.id
  153. ],
  154. 'group_ids': []
  155. },
  156. 'write': {
  157. 'user_ids': [],
  158. 'group_ids': []
  159. }
  160. })
  161. })
  162. assert_equal(0, json.loads(response.content)['status'], response.content)
  163. response = self.client.get('/home')
  164. assert_true(doc_id in json.loads(response.context['json_documents']))
  165. response = self.client_not_me.get('/home')
  166. assert_true(doc_id in json.loads(response.context['json_documents']))
  167. # Un-share
  168. response = self.client.post("/desktop/api/doc/update_permissions", {
  169. 'doc_id': doc.id,
  170. 'data': json.dumps({
  171. 'read': {
  172. 'user_ids': [
  173. self.user.id
  174. ],
  175. 'group_ids': []
  176. },
  177. 'write': {
  178. 'user_ids': [],
  179. 'group_ids': []
  180. }
  181. })
  182. })
  183. assert_equal(0, json.loads(response.content)['status'], response.content)
  184. response = self.client.get('/home')
  185. assert_true(doc_id in json.loads(response.context['json_documents']))
  186. response = self.client_not_me.get('/home')
  187. assert_false(doc_id in json.loads(response.context['json_documents']))
  188. # Share by group
  189. default_group = get_default_user_group()
  190. response = self.client.post("/desktop/api/doc/update_permissions", {
  191. 'doc_id': doc.id,
  192. 'data': json.dumps({
  193. 'read': {
  194. 'user_ids': [
  195. self.user.id
  196. ],
  197. 'group_ids': [
  198. default_group.id
  199. ]
  200. },
  201. 'write': {
  202. 'user_ids': [],
  203. 'group_ids': []
  204. }
  205. })
  206. })
  207. assert_equal(0, json.loads(response.content)['status'], response.content)
  208. response = self.client.get('/home')
  209. assert_true(doc_id in json.loads(response.context['json_documents']))
  210. response = self.client_not_me.get('/home')
  211. assert_true(doc_id in json.loads(response.context['json_documents']))
  212. # Un-share
  213. response = self.client.post("/desktop/api/doc/update_permissions", {
  214. 'doc_id': doc.id,
  215. 'data': json.dumps({
  216. 'read': {
  217. 'user_ids': [
  218. self.user.id
  219. ],
  220. 'group_ids': []
  221. },
  222. 'write': {
  223. 'user_ids': [],
  224. 'group_ids': []
  225. }
  226. })
  227. })
  228. assert_equal(0, json.loads(response.content)['status'], response.content)
  229. response = self.client.get('/home')
  230. assert_true(doc_id in json.loads(response.context['json_documents']))
  231. response = self.client_not_me.get('/home')
  232. assert_false(doc_id in json.loads(response.context['json_documents']))
  233. # Modify by user
  234. response = self.client.post("/desktop/api/doc/update_permissions", {
  235. 'doc_id': doc.id,
  236. 'data': json.dumps({
  237. 'read': {
  238. 'user_ids': [
  239. self.user.id
  240. ],
  241. 'group_ids': []
  242. },
  243. 'write': {
  244. 'user_ids': [
  245. self.user_not_me.id
  246. ],
  247. 'group_ids': []
  248. }
  249. })
  250. })
  251. assert_equal(0, json.loads(response.content)['status'], response.content)
  252. response = self.client.get('/home')
  253. assert_true(doc_id in json.loads(response.context['json_documents']))
  254. response = self.client_not_me.get('/home')
  255. assert_true(doc_id in json.loads(response.context['json_documents']))
  256. # Un-share
  257. response = self.client.post("/desktop/api/doc/update_permissions", {
  258. 'doc_id': doc.id,
  259. 'data': json.dumps({
  260. 'read': {
  261. 'user_ids': [
  262. self.user.id
  263. ],
  264. 'group_ids': []
  265. },
  266. 'write': {
  267. 'user_ids': [],
  268. 'group_ids': []
  269. }
  270. })
  271. })
  272. assert_equal(0, json.loads(response.content)['status'], response.content)
  273. response = self.client.get('/home')
  274. assert_true(doc_id in json.loads(response.context['json_documents']))
  275. response = self.client_not_me.get('/home')
  276. assert_false(doc_id in json.loads(response.context['json_documents']))
  277. # Modify by group
  278. response = self.client.post("/desktop/api/doc/update_permissions", {
  279. 'doc_id': doc.id,
  280. 'data': json.dumps({
  281. 'read': {
  282. 'user_ids': [
  283. self.user.id
  284. ],
  285. 'group_ids': []
  286. },
  287. 'write': {
  288. 'user_ids': [],
  289. 'group_ids': [
  290. default_group.id
  291. ]
  292. }
  293. })
  294. })
  295. assert_equal(0, json.loads(response.content)['status'], response.content)
  296. response = self.client.get('/home')
  297. assert_true(doc_id in json.loads(response.context['json_documents']))
  298. response = self.client_not_me.get('/home')
  299. assert_true(doc_id in json.loads(response.context['json_documents']))
  300. # Un-share
  301. response = self.client.post("/desktop/api/doc/update_permissions", {
  302. 'doc_id': doc.id,
  303. 'data': json.dumps({
  304. 'read': {
  305. 'user_ids': [
  306. self.user.id
  307. ],
  308. 'group_ids': []
  309. },
  310. 'write': {
  311. 'user_ids': [],
  312. 'group_ids': []
  313. }
  314. })
  315. })
  316. assert_equal(0, json.loads(response.content)['status'], response.content)
  317. response = self.client.get('/home')
  318. assert_true(doc_id in json.loads(response.context['json_documents']))
  319. response = self.client_not_me.get('/home')
  320. assert_false(doc_id in json.loads(response.context['json_documents']))