github.com/greenpau/go-authcrunch@v1.1.4/internal/testutils/identity.go (about)

     1  // Copyright 2022 Paul Greenberg greenpau@outlook.com
     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  
    15  package testutils
    16  
    17  import (
    18  	"github.com/greenpau/go-authcrunch/internal/tests"
    19  	"github.com/greenpau/go-authcrunch/pkg/identity"
    20  	"github.com/greenpau/go-authcrunch/pkg/requests"
    21  	"path/filepath"
    22  )
    23  
    24  // CreateTestDatabase returns database instance.
    25  func CreateTestDatabase(s string) (*identity.Database, error) {
    26  	tmpDir, err := tests.TempDir(s)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	reqs := []*requests.Request{
    31  		{
    32  			User: requests.User{
    33  				Username: tests.TestUser1,
    34  				Password: tests.TestPwd1,
    35  				Email:    tests.TestEmail1,
    36  				FullName: tests.TestFullName1,
    37  				Roles:    tests.TestRoles1,
    38  			},
    39  		},
    40  		{
    41  			User: requests.User{
    42  				Username: tests.TestUser2,
    43  				Password: tests.TestPwd2,
    44  				Email:    tests.TestEmail2,
    45  				FullName: tests.TestFullName2,
    46  				Roles:    tests.TestRoles2,
    47  			},
    48  		},
    49  	}
    50  
    51  	db, err := identity.NewDatabase(filepath.Join(tmpDir, "user_db.json"))
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  
    56  	for _, req := range reqs {
    57  		if err := db.AddUser(req); err != nil {
    58  			return nil, err
    59  		}
    60  	}
    61  	return db, nil
    62  }
    63  
    64  // CreateEmptyTestDatabase returns empty database instance.
    65  func CreateEmptyTestDatabase(s string) (*identity.Database, error) {
    66  	tmpDir, err := tests.TempDir(s)
    67  	if err != nil {
    68  		return nil, err
    69  	}
    70  	return identity.NewDatabase(filepath.Join(tmpDir, "user_db.json"))
    71  }