blob: 270b16e335ea1940f1dd14491032599a947478a6 [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Bakere72e7612017-02-20 10:07:09 -080017import sys
18sys.path.append("..")
19
20from xosapi import xos_grpc_client
21
22def test_callback():
23 print "TEST: orm_user_crud"
24
25 c = xos_grpc_client.coreclient
26
27 # create a new user and save it
28 u=c.xos_orm.User.objects.new()
29 assert(u.id==0)
30 import random, string
31 u.email=''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(10))
32 u.site=c.xos_orm.Site.objects.all()[0]
33 u.save()
34
35 # when we created the user, he should be assigned an id
36 orig_id = u.id
37 assert(orig_id!=0)
38
39 # invalidate u.site so it's reloaded from the server
40 u.invalidate_cache("site")
41
42 # site object should be populated
43 assert(u.site is not None)
44
45 # site object should have a backpointer to user
46 u_all = u.site.users.all()
47 u_all = [x for x in u_all if x.email == u.email]
48 assert(len(u_all)==1)
49
50 # update the user
51 u.password="foobar"
52 u.save()
53
54 # update should not have changed it
55 assert(u.id==orig_id)
56
57 # check a listall and make sure the user is listed
58 u_all = c.xos_orm.User.objects.all()
59 u_all = [x for x in u_all if x.email == u.email]
60 assert(len(u_all)==1)
61 u2 = u_all[0]
62 assert(u2.id == u.id)
63
64 # get and make sure the password was updated
65 u3 = c.xos_orm.User.objects.get(id=orig_id)
66 assert(u3.password=="foobar")
67
Scott Baker57c74822017-02-23 11:13:04 -080068 # try a partial update
69 u3.password = "should_not_change"
70 u3.firstname = "new_first_name"
71 u3.lastname = "new_last_name"
72 u3.save(update_fields = ["firstname", "lastname"])
73
74 # get and make sure the password was not updated, but first and last name were
75 u4 = c.xos_orm.User.objects.get(id=orig_id)
76 assert(u4.password=="foobar")
77 assert(u4.firstname == "new_first_name")
78 assert(u4.lastname == "new_last_name")
79
Scott Bakere72e7612017-02-20 10:07:09 -080080 # delete the user
Scott Baker57c74822017-02-23 11:13:04 -080081 u4.delete()
Scott Bakere72e7612017-02-20 10:07:09 -080082
83 # make sure it is deleted
84 u_all = c.xos_orm.User.objects.all()
85 u_all = [x for x in u_all if x.email == u.email]
86 assert(len(u_all)==0)
87
88 print " okay"
89
90xos_grpc_client.start_api_parseargs(test_callback)
91