github.com/e154/smart-home@v0.17.2-0.20240311175135-e530a6e5cd45/tests/models/user_test.go (about)

     1  // This file is part of the Smart Home
     2  // Program complex distribution https://github.com/e154/smart-home
     3  // Copyright (C) 2016-2023, Filippov Alex
     4  //
     5  // This library is free software: you can redistribute it and/or
     6  // modify it under the terms of the GNU Lesser General Public
     7  // License as published by the Free Software Foundation; either
     8  // version 3 of the License, or (at your option) any later version.
     9  //
    10  // This library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    13  // Library General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public
    16  // License along with this library.  If not, see
    17  // <https://www.gnu.org/licenses/>.
    18  
    19  package models
    20  
    21  import (
    22  	"context"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/e154/smart-home/adaptors"
    27  	m "github.com/e154/smart-home/models"
    28  	"github.com/e154/smart-home/system/access_list"
    29  	"github.com/e154/smart-home/system/migrations"
    30  	"github.com/go-playground/validator/v10"
    31  	. "github.com/smartystreets/goconvey/convey"
    32  )
    33  
    34  func TestUser(t *testing.T) {
    35  
    36  	Convey("add user", t, func(ctx C) {
    37  		_ = container.Invoke(func(adaptors *adaptors.Adaptors,
    38  			migrations *migrations.Migrations,
    39  			accessList access_list.AccessListService) {
    40  
    41  			// clear database
    42  			_ = migrations.Purge()
    43  
    44  			// add role
    45  			userRole := &m.Role{
    46  				Name: "user_role",
    47  			}
    48  			err := adaptors.Role.Add(context.Background(), userRole)
    49  			So(err, ShouldBeNil)
    50  
    51  			var counter int
    52  			for pack, item := range *accessList.List(context.Background()) {
    53  				for right := range item {
    54  					if strings.Contains(right, "read") ||
    55  						strings.Contains(right, "view") ||
    56  						strings.Contains(right, "show") {
    57  						permission := &m.Permission{
    58  							RoleName:    userRole.Name,
    59  							PackageName: pack,
    60  							LevelName:   right,
    61  						}
    62  
    63  						counter++
    64  						_, err = adaptors.Permission.Add(context.Background(), permission)
    65  						So(err, ShouldBeNil)
    66  					}
    67  				}
    68  			}
    69  
    70  			const (
    71  				nickname = "user"
    72  				email    = "email@mail.com"
    73  			)
    74  
    75  			// add user
    76  			user := &m.User{
    77  				Nickname: nickname,
    78  				RoleName: "user_role",
    79  				Email:    email,
    80  				Lang:     "en",
    81  				Meta: []*m.UserMeta{
    82  					{
    83  						Key:   "phone1",
    84  						Value: "+18004001234",
    85  					},
    86  				},
    87  			}
    88  			err = user.SetPass("123456")
    89  			So(err, ShouldBeNil)
    90  
    91  			validate := validator.New()
    92  			err = validate.Struct(user)
    93  			So(err, ShouldBeNil)
    94  
    95  			user.Id, err = adaptors.User.Add(context.Background(), user)
    96  			So(err, ShouldBeNil)
    97  
    98  			user, err = adaptors.User.GetById(context.Background(), user.Id)
    99  			So(err, ShouldBeNil)
   100  
   101  			//debug.Println(user)
   102  			//fmt.Println("----")
   103  
   104  			So(user.Nickname, ShouldEqual, nickname)
   105  			So(user.Email, ShouldEqual, email)
   106  			So(user.Role, ShouldNotBeNil)
   107  			So(user.Role.Name, ShouldEqual, "user_role")
   108  		})
   109  	})
   110  }