blob: 96f38d6cdb3246f5ba8d1824a20e7ac03596af6a [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001# Copyright 2017-present Open Networking Foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
Zack Williams045b63d2019-01-22 16:30:57 -070015from __future__ import print_function
16from xosapi import xos_grpc_client
Scott Bakere72e7612017-02-20 10:07:09 -080017import sys
Zack Williams045b63d2019-01-22 16:30:57 -070018
Scott Bakere72e7612017-02-20 10:07:09 -080019sys.path.append("..")
20
Scott Bakere72e7612017-02-20 10:07:09 -080021
22def test_callback():
Zack Williams045b63d2019-01-22 16:30:57 -070023 print("TEST: orm_user_crud")
Scott Bakere72e7612017-02-20 10:07:09 -080024
25 c = xos_grpc_client.coreclient
26
27 # create a new user and save it
Zack Williams045b63d2019-01-22 16:30:57 -070028 u = c.xos_orm.User.objects.new()
29 assert u.id == 0
30 import random
31 import string
32
33 u.email = "".join(
34 random.choice(string.ascii_uppercase + string.digits) for _ in range(10)
35 )
36 u.site = c.xos_orm.Site.objects.all()[0]
Scott Bakere72e7612017-02-20 10:07:09 -080037 u.save()
38
39 # when we created the user, he should be assigned an id
40 orig_id = u.id
Zack Williams045b63d2019-01-22 16:30:57 -070041 assert orig_id != 0
Scott Bakere72e7612017-02-20 10:07:09 -080042
43 # invalidate u.site so it's reloaded from the server
44 u.invalidate_cache("site")
45
46 # site object should be populated
Zack Williams045b63d2019-01-22 16:30:57 -070047 assert u.site is not None
Scott Bakere72e7612017-02-20 10:07:09 -080048
49 # site object should have a backpointer to user
50 u_all = u.site.users.all()
51 u_all = [x for x in u_all if x.email == u.email]
Zack Williams045b63d2019-01-22 16:30:57 -070052 assert len(u_all) == 1
Scott Bakere72e7612017-02-20 10:07:09 -080053
54 # update the user
Zack Williams045b63d2019-01-22 16:30:57 -070055 u.password = "foobar"
Scott Bakere72e7612017-02-20 10:07:09 -080056 u.save()
57
58 # update should not have changed it
Zack Williams045b63d2019-01-22 16:30:57 -070059 assert u.id == orig_id
Scott Bakere72e7612017-02-20 10:07:09 -080060
61 # check a listall and make sure the user is listed
62 u_all = c.xos_orm.User.objects.all()
63 u_all = [x for x in u_all if x.email == u.email]
Zack Williams045b63d2019-01-22 16:30:57 -070064 assert len(u_all) == 1
Scott Bakere72e7612017-02-20 10:07:09 -080065 u2 = u_all[0]
Zack Williams045b63d2019-01-22 16:30:57 -070066 assert u2.id == u.id
Scott Bakere72e7612017-02-20 10:07:09 -080067
68 # get and make sure the password was updated
69 u3 = c.xos_orm.User.objects.get(id=orig_id)
Zack Williams045b63d2019-01-22 16:30:57 -070070 assert u3.password == "foobar"
Scott Bakere72e7612017-02-20 10:07:09 -080071
Scott Baker57c74822017-02-23 11:13:04 -080072 # try a partial update
73 u3.password = "should_not_change"
74 u3.firstname = "new_first_name"
75 u3.lastname = "new_last_name"
Zack Williams045b63d2019-01-22 16:30:57 -070076 u3.save(update_fields=["firstname", "lastname"])
Scott Baker57c74822017-02-23 11:13:04 -080077
78 # get and make sure the password was not updated, but first and last name were
79 u4 = c.xos_orm.User.objects.get(id=orig_id)
Zack Williams045b63d2019-01-22 16:30:57 -070080 assert u4.password == "foobar"
81 assert u4.firstname == "new_first_name"
82 assert u4.lastname == "new_last_name"
Scott Baker57c74822017-02-23 11:13:04 -080083
Scott Bakere72e7612017-02-20 10:07:09 -080084 # delete the user
Scott Baker57c74822017-02-23 11:13:04 -080085 u4.delete()
Scott Bakere72e7612017-02-20 10:07:09 -080086
87 # make sure it is deleted
88 u_all = c.xos_orm.User.objects.all()
89 u_all = [x for x in u_all if x.email == u.email]
Zack Williams045b63d2019-01-22 16:30:57 -070090 assert len(u_all) == 0
Scott Bakere72e7612017-02-20 10:07:09 -080091
Zack Williams045b63d2019-01-22 16:30:57 -070092 print(" okay")
93
Scott Bakere72e7612017-02-20 10:07:09 -080094
95xos_grpc_client.start_api_parseargs(test_callback)