Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 15 | from __future__ import print_function |
| 16 | from xosapi import xos_grpc_client |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 17 | import sys |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 18 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 19 | sys.path.append("..") |
| 20 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 21 | |
| 22 | def test_callback(): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 23 | print("TEST: orm_user_crud") |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 24 | |
| 25 | c = xos_grpc_client.coreclient |
| 26 | |
| 27 | # create a new user and save it |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 28 | 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 Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 37 | u.save() |
| 38 | |
| 39 | # when we created the user, he should be assigned an id |
| 40 | orig_id = u.id |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 41 | assert orig_id != 0 |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 42 | |
| 43 | # invalidate u.site so it's reloaded from the server |
| 44 | u.invalidate_cache("site") |
| 45 | |
| 46 | # site object should be populated |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | assert u.site is not None |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 48 | |
| 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 52 | assert len(u_all) == 1 |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 53 | |
| 54 | # update the user |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 55 | u.password = "foobar" |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 56 | u.save() |
| 57 | |
| 58 | # update should not have changed it |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 59 | assert u.id == orig_id |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 60 | |
| 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 64 | assert len(u_all) == 1 |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 65 | u2 = u_all[0] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 66 | assert u2.id == u.id |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 67 | |
| 68 | # get and make sure the password was updated |
| 69 | u3 = c.xos_orm.User.objects.get(id=orig_id) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 70 | assert u3.password == "foobar" |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 71 | |
Scott Baker | 57c7482 | 2017-02-23 11:13:04 -0800 | [diff] [blame] | 72 | # try a partial update |
| 73 | u3.password = "should_not_change" |
| 74 | u3.firstname = "new_first_name" |
| 75 | u3.lastname = "new_last_name" |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 76 | u3.save(update_fields=["firstname", "lastname"]) |
Scott Baker | 57c7482 | 2017-02-23 11:13:04 -0800 | [diff] [blame] | 77 | |
| 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 80 | assert u4.password == "foobar" |
| 81 | assert u4.firstname == "new_first_name" |
| 82 | assert u4.lastname == "new_last_name" |
Scott Baker | 57c7482 | 2017-02-23 11:13:04 -0800 | [diff] [blame] | 83 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 84 | # delete the user |
Scott Baker | 57c7482 | 2017-02-23 11:13:04 -0800 | [diff] [blame] | 85 | u4.delete() |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 86 | |
| 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 Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 90 | assert len(u_all) == 0 |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 91 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 92 | print(" okay") |
| 93 | |
Scott Baker | e72e761 | 2017-02-20 10:07:09 -0800 | [diff] [blame] | 94 | |
| 95 | xos_grpc_client.start_api_parseargs(test_callback) |