PageRenderTime 55ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/crgappspanel/tests.py

https://github.com/ron-panduwana/test_gae
Python | 511 lines | 465 code | 38 blank | 8 comment | 15 complexity | 72c7e96d943d03e9b83d72f9e18525ea MD5 | raw file
  1. from __future__ import with_statement
  2. import datetime
  3. import logging
  4. import os
  5. import unittest
  6. from appengine_django.models import BaseModel
  7. from google.appengine.ext import db
  8. from gdata.apps.service import AppsForYourDomainException
  9. from crgappspanel.models import *
  10. from crlib.gdata_wrapper import GDataQuery
  11. from crlib.mappers import MAIN_TYPES, PHONE_TYPES, ORGANIZATION_TYPES, \
  12. WEBSITE_TYPES
  13. from crlib import cache
  14. from crlib.models import GDataIndex, UserCache
  15. from crauth.models import AppsDomain
  16. from crauth.users import _set_current_user
  17. os.environ['SERVER_NAME'] = 'localhost'
  18. os.environ['SERVER_PORT'] = '8000'
  19. os.environ['USER_EMAIL'] = 'test@example.com'
  20. os.environ['USER_IS_ADMIN'] = '1'
  21. TEST_DOMAIN = 'red.lab.cloudreach.co.uk'
  22. with open('google_apps.txt') as f:
  23. lines = f.readlines()
  24. email, password, domain = [line.strip() for line in lines]
  25. _set_current_user(email, domain)
  26. class BaseGDataTestCase(unittest.TestCase):
  27. USER_NAME = 'test'
  28. USER_GIVEN_NAME = 'Test'
  29. USER_FAMILY_NAME = 'User2'
  30. NUMBER_OF_USERS = 4
  31. NUMBER_OF_ADMINS = 2
  32. def setUp(self):
  33. AppsDomain.get_or_insert(
  34. key_name=domain,
  35. domain=domain,
  36. admin_email=email,
  37. admin_password=password)
  38. idx = GDataIndex.get_by_key_name('%s:GAUser' % TEST_DOMAIN)
  39. if idx:
  40. return
  41. logging.info('Preparing cache...')
  42. cache.prepare_indexes(TEST_DOMAIN)
  43. indexes = GDataIndex.all().fetch(100)
  44. for index in indexes:
  45. key_parts = index.key().name().split(':')
  46. key_name = ':'.join(key_parts[:2])
  47. if len(key_parts) == 3:
  48. page = key_parts[2]
  49. else:
  50. page = 0
  51. cache.update_cache({
  52. 'key_name': key_name,
  53. 'page': page,
  54. })
  55. logging.info('Done.')
  56. class ProvisioningAPITestCase(BaseGDataTestCase):
  57. def testGetAllUsers(self):
  58. for user in GAUser.all().fetch(100):
  59. print user
  60. def testGetUserByName(self):
  61. user = GAUser.get_by_key_name(self.USER_NAME)
  62. self.assertNotEqual(user, None)
  63. def testSaveUser(self):
  64. user = GAUser.get_by_key_name(self.USER_NAME)
  65. user.given_name = 'other_name'
  66. user.put()
  67. user.given_name = self.USER_GIVEN_NAME
  68. user.put()
  69. def testChangePassword(self):
  70. user = GAUser.get_by_key_name(self.USER_NAME)
  71. user.password = 'new_password'
  72. user.put()
  73. def testNewUser(self):
  74. from crlib.errors import DomainUserLimitExceededError
  75. new_user = GAUser(
  76. user_name=self.USER_NAME,
  77. given_name=self.USER_GIVEN_NAME,
  78. family_name=self.USER_FAMILY_NAME,
  79. password='some_password',
  80. )
  81. self.assertRaises(DomainUserLimitExceededError, new_user.put)
  82. def testCreateNickname(self):
  83. user = GAUser.get_by_key_name(self.USER_NAME)
  84. nickname = GANickname(
  85. nickname='testnick',
  86. user=user)
  87. nickname.put()
  88. nickname = GANickname.get_by_key_name('testnick')
  89. self.assertTrue(nickname is not None)
  90. self.assertEqual(nickname.user.user_name, user.user_name)
  91. def testDeleteNickname(self):
  92. user = GAUser.get_by_key_name(self.USER_NAME)
  93. nickname = GANickname(
  94. nickname='to_be_deleted',
  95. user=user)
  96. nickname.put()
  97. nickname.delete()
  98. nickname = GANickname.get_by_key_name('to_be_deleted')
  99. self.assertEqual(nickname, None)
  100. def testNicknamesCollection(self):
  101. user = GAUser.get_by_key_name(self.USER_NAME)
  102. for nickname in user.nicknames:
  103. print nickname
  104. def testRenameUser(self):
  105. # We need to handle nicknames first
  106. self.assertTrue(False)
  107. user = GAUser.get_by_key_name(self.USER_NAME)
  108. user.user_name = 'new_user'
  109. user.put()
  110. def testMultipleChanges(self):
  111. # We need to handle nicknames first
  112. self.assertTrue(False)
  113. user = GAUser.get_by_key_name(self.USER_NAME)
  114. user.user_name = 'new_user'
  115. user.given_name = 'Test'
  116. user.password = 'new_password'
  117. user.put()
  118. #def testReferencePropertyOnNormalModel(self):
  119. # user = GAUser.get_by_key_name(self.USER_NAME)
  120. # test_model = TestModel(user=user)
  121. # test_model.put()
  122. # test_model = TestModel.all().get()
  123. # self.assertEqual(user, test_model.user)
  124. def testGDataQueryRE(self):
  125. regexp = GDataQuery._PROPERTY_RE
  126. correct_properties = (
  127. 'property =', 'property=',
  128. 'property', 'property_with_underscore =',
  129. 'property >', 'property >=',
  130. 'property <', 'property <=',
  131. 'property in', 'property IN',
  132. 'property !=', 'property',
  133. )
  134. for property in correct_properties:
  135. match = regexp.match(property)
  136. self.assertNotEqual(match, None)
  137. incorrect_properties = (
  138. 'property with spaces', 'property <>',
  139. )
  140. for property in incorrect_properties:
  141. self.assertEqual(regexp.match(property), None)
  142. def testGDataQueryFilter(self):
  143. users = GAUser.all().filter('admin', True).fetch(100)
  144. self.assertEqual(len(users), self.NUMBER_OF_ADMINS)
  145. users = GAUser.all().filter('admin', True).filter(
  146. 'user_name', 'kamil').fetch(100)
  147. self.assertEqual(len(users), 1)
  148. def testGDataQueryIter(self):
  149. i = 0
  150. for user in GAUser.all():
  151. i += 1
  152. self.assertEqual(i, self.NUMBER_OF_USERS)
  153. def testGDataQueryOrder(self):
  154. for user in GAUser.all().order('admin').order('user_name'):
  155. print user
  156. class ProvisioningAPIGroupsTestCase(BaseGDataTestCase):
  157. def testCreateGroup(self):
  158. existing = GAGroup.all().filter(
  159. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  160. if existing is not None:
  161. existing.delete()
  162. group = GAGroup(
  163. id='some_group@' + TEST_DOMAIN,
  164. name='some group',
  165. description='this is some group',
  166. email_permission='Owner').save()
  167. def testCreateGroupWithMissingProperties(self):
  168. existing = GAGroup.all().filter(
  169. 'id', 'some_group@' + TEST_DOMAIN).get()
  170. if existing is not None:
  171. existing.delete()
  172. group = GAGroup(
  173. id='some_group@' + TEST_DOMAIN,
  174. name='some group',
  175. description='',
  176. email_permission='Owner').save()
  177. def testRetrieveAll(self):
  178. for group in GAGroup.all():
  179. print group
  180. def testRetrieveMembers(self):
  181. group = GAGroup.all().get()
  182. print group.key()
  183. for member in group.members:
  184. print member
  185. print member.to_user()
  186. print member.is_group()
  187. def testAddUserToGroups(self):
  188. user = GAUser.get_by_key_name(self.USER_NAME)
  189. group_ids = (
  190. 'some_group@red.lab.cloudreach.co.uk',
  191. 'sap@red.lab.cloudreach.co.uk',
  192. )
  193. GAGroup.add_user_to_groups(user, group_ids)
  194. member = GAGroupMember.from_user(user)
  195. for group_id in group_ids:
  196. group = GAGroup.get_by_key_name(group_id)
  197. self.assertTrue(member in group.members)
  198. def testRetrieveGroupsByMember(self):
  199. user = GAUser.get_by_key_name(self.USER_NAME)
  200. member = GAGroupMember.from_user(user)
  201. groups = GAGroup.all().filter('members', member).fetch(100)
  202. for group in groups:
  203. print group
  204. def testUpdateGroup(self):
  205. group = GAGroup.all().filter(
  206. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  207. if group is None:
  208. group = GAGroup(
  209. id='some_group@' + TEST_DOMAIN,
  210. name='some group',
  211. description='this is some group',
  212. email_permission='Owner').save()
  213. group.name = 'new name'
  214. user = GAUser.get_by_key_name(self.USER_NAME)
  215. member = GAGroupMember.from_user(user)
  216. group.members.append(member)
  217. group.save()
  218. group = GAGroup.all().filter(
  219. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  220. self.assertEqual(group.name, 'new name')
  221. self.assertTrue(member in group.members)
  222. group.name = 'some group'
  223. group.members = []
  224. group.save()
  225. group = GAGroup.all().filter(
  226. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  227. self.assertEqual(len(group.members), 0)
  228. def testOwners(self):
  229. group = GAGroup.all().filter(
  230. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  231. if group is None:
  232. group = GAGroup(
  233. id='some_group@' + TEST_DOMAIN,
  234. name='some group',
  235. description='this is some group',
  236. email_permission='Owner').save()
  237. user = GAUser.get_by_key_name(self.USER_NAME)
  238. owner = GAGroupOwner.from_user(user)
  239. group.owners.append(owner)
  240. group.save()
  241. group = GAGroup.all().filter(
  242. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  243. self.assertTrue(owner in group.owners)
  244. group.owners = []
  245. group.save()
  246. group = GAGroup.all().filter(
  247. 'id', 'some_group@red.lab.cloudreach.co.uk').get()
  248. self.assertEqual(len(group.owners), 0)
  249. class SharedContactsAPITestCase(BaseGDataTestCase):
  250. def testCreateSharedContact(self):
  251. contact = SharedContact.all().filter('title', 'Test Contact').get()
  252. if contact:
  253. contact.delete()
  254. email = Email(
  255. address='test@example.com',
  256. rel=MAIN_TYPES[0][0],
  257. primary=True)
  258. email.save()
  259. phone_number = PhoneNumber(
  260. number='555-123-456',
  261. rel=PHONE_TYPES[0][0],
  262. primary=True)
  263. phone_number.save()
  264. name = Name(full_name='Test Contact')
  265. name.save()
  266. organization = Organization(
  267. name='Some Test Organization',
  268. rel=ORGANIZATION_TYPES[0][0],
  269. primary=True)
  270. organization.save()
  271. website = Website(
  272. href='http://www.cloudreach.co.uk/',
  273. primary=True,
  274. rel=WEBSITE_TYPES[0][0])
  275. website.save()
  276. contact = SharedContact(
  277. name=name,
  278. birthday=datetime.date(1990, 10, 10),
  279. emails=[email],
  280. phone_numbers=[phone_number],
  281. websites=[website],
  282. organization=organization)
  283. contact.save()
  284. contact.delete()
  285. def testGetByKeyName(self):
  286. contact_1 = SharedContact.all().get()
  287. key = contact_1.key()
  288. contact_2 = SharedContact.get_by_key_name(key)
  289. self.assertEqual(contact_1, contact_2)
  290. def testUpdateSharedContact(self):
  291. contact = SharedContact.all().filter('title', 'Updated Name').get()
  292. if contact:
  293. logging.warning('Had Updated Name contact')
  294. logging.warning('contact: %s' % str(contact))
  295. contact.delete()
  296. email = Email(
  297. address='updated@example.com',
  298. rel=MAIN_TYPES[0][0],
  299. primary=True)
  300. email.save()
  301. name = Name(full_name='Updated Contact')
  302. name.save()
  303. contact = SharedContact(
  304. name=name,
  305. emails=[email])
  306. contact.save()
  307. contact = SharedContact.all().filter('title', 'Updated Contact').get()
  308. self.assertFalse(contact is None)
  309. contact.name.full_name = 'Updated Name'
  310. contact.name.save()
  311. contact.save()
  312. contact.delete()
  313. def testIterSharedContacts(self):
  314. for contact in SharedContact.all():
  315. print contact
  316. self.assertTrue(isinstance(contact, SharedContact))
  317. def testSharedContactExtendedProperty(self):
  318. contact = SharedContact.all().filter(
  319. 'title', 'ExtendedProperty Contact').get()
  320. if contact:
  321. contact.delete()
  322. email = Email(
  323. address='extended@example.com',
  324. rel=MAIN_TYPES[0][0],
  325. primary=True)
  326. email.save()
  327. name = Name(full_name='ExtendedProperty Contact')
  328. name.save()
  329. contact = SharedContact(
  330. name=name,
  331. emails=[email],
  332. extended_properties={
  333. 'some_name': 'some_val',
  334. })
  335. contact.save()
  336. contact = SharedContact.all().filter(
  337. 'title', 'ExtendedProperty Contact').get()
  338. self.assertEqual(contact.extended_properties['some_name'], 'some_val')
  339. class EmailSettingsAPITestCase(BaseGDataTestCase):
  340. def testCreateLabel(self):
  341. user = GAUser.get_by_key_name(self.USER_NAME)
  342. self.assertNotEqual(user, None)
  343. response = user.email_settings.create_label('test_label')
  344. self.assertEqual(response, {'label': 'test_label'})
  345. def testDisableWebClip(self):
  346. user = GAUser.get_by_key_name(self.USER_NAME)
  347. self.assertNotEqual(user, None)
  348. response = user.email_settings.update_web_clip_settings(False)
  349. self.assertEqual(response, {'enable': 'false'})
  350. class CalendarResourceAPITestCase(BaseGDataTestCase):
  351. def testCreateDuplicateResource(self):
  352. from crlib.errors import EntityExistsError
  353. def create_duplicate():
  354. for i in range(2):
  355. resource = CalendarResource(
  356. id='DUPLICATE_RESOURCE',
  357. common_name='Test Resource',
  358. type='Meeting Room',
  359. description='',
  360. ).save()
  361. self.assertRaises(EntityExistsError, create_duplicate)
  362. def testGetAllResources(self):
  363. resources = CalendarResource.all().fetch(100)
  364. for resource in resources:
  365. print resource
  366. print resource.id
  367. print resource.description
  368. print resource.common_name
  369. print resource.type
  370. def testCreateResource(self):
  371. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  372. if resource is not None:
  373. resource.delete()
  374. resource = CalendarResource(
  375. id='TEST_RESOURCE',
  376. common_name='Test Resource',
  377. type='Meeting Room',
  378. description='',
  379. ).save()
  380. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  381. self.assertNotEqual(resource, None)
  382. resource.delete()
  383. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  384. self.assertEqual(resource, None)
  385. def testUpdateResource(self):
  386. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  387. if resource is None:
  388. resource = CalendarResource(
  389. id='TEST_RESOURCE',
  390. common_name='Test Resource',
  391. type='Meeting Room',
  392. description='',
  393. ).save()
  394. resource.common_name = 'NEW_COMMON_NAME'
  395. resource.description = 'NEW_DESCRIPTION'
  396. resource.save()
  397. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  398. self.assertEqual(resource.common_name, 'NEW_COMMON_NAME')
  399. self.assertEqual(resource.description, 'NEW_DESCRIPTION')
  400. resource.delete()
  401. def testUpdateResourceWithNoDescription(self):
  402. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  403. if resource is None:
  404. resource = CalendarResource(
  405. id='TEST_RESOURCE',
  406. common_name='Test Resource',
  407. type='Meeting Room',
  408. description='',
  409. ).save()
  410. resource.description = 'SOME_DESCRIPTION'
  411. resource.save()
  412. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  413. self.assertEqual(resource.description, 'SOME_DESCRIPTION')
  414. resource.description = ''
  415. resource.save()
  416. resource = CalendarResource.get_by_key_name('TEST_RESOURCE')
  417. self.assertNotEqual(resource, None)
  418. self.assertEqual(resource.description, None)