github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/model/user_test.go (about)

     1  package model_test
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/hashicorp/go-multierror"
     7  	. "github.com/onsi/ginkgo/v2"
     8  	. "github.com/onsi/gomega"
     9  
    10  	"github.com/pyroscope-io/pyroscope/pkg/model"
    11  )
    12  
    13  var _ = Describe("User password verification", func() {
    14  	Context("when password hashed", func() {
    15  		var (
    16  			p string
    17  			h []byte
    18  		)
    19  
    20  		BeforeEach(func() {
    21  			p = "qwerty"
    22  			h = model.MustPasswordHash(p)
    23  		})
    24  
    25  		It("produces unique hash", func() {
    26  			Expect(h).ToNot(Equal(model.MustPasswordHash(p)))
    27  		})
    28  
    29  		Context("at verification", func() {
    30  			Context("when password matches", func() {
    31  				It("reports no error", func() {
    32  					Expect(model.VerifyPassword(h, p)).ToNot(HaveOccurred())
    33  				})
    34  			})
    35  
    36  			Context("when password does not match", func() {
    37  				It("reports error", func() {
    38  					Expect(model.VerifyPassword(h, "xxx")).To(HaveOccurred())
    39  				})
    40  			})
    41  		})
    42  	})
    43  })
    44  
    45  var _ = Describe("User validation", func() {
    46  	Describe("CreateUserParams", func() {
    47  		type createUserParamsCase struct {
    48  			params model.CreateUserParams
    49  			err    error
    50  		}
    51  
    52  		DescribeTable("CreateUserParams cases",
    53  			func(c createUserParamsCase) {
    54  				expectErrOrNil(c.params.Validate(), c.err)
    55  			},
    56  
    57  			Entry("valid params", createUserParamsCase{
    58  				params: model.CreateUserParams{
    59  					Name:     "johndoe",
    60  					FullName: model.String("John Doe"),
    61  					Email:    model.String("john@example.com"),
    62  					Password: "qwerty",
    63  					Role:     model.ReadOnlyRole,
    64  				},
    65  			}),
    66  
    67  			Entry("name is too long", createUserParamsCase{
    68  				params: model.CreateUserParams{
    69  					Name: strings.Repeat("X", 256),
    70  				},
    71  				err: &multierror.Error{Errors: []error{
    72  					model.ErrUserNameTooLong,
    73  					model.ErrUserPasswordEmpty,
    74  					model.ErrRoleUnknown,
    75  				}},
    76  			}),
    77  
    78  			Entry("full name is too long", createUserParamsCase{
    79  				params: model.CreateUserParams{
    80  					FullName: model.String(strings.Repeat("X", 256)),
    81  				},
    82  				err: &multierror.Error{Errors: []error{
    83  					model.ErrUserNameEmpty,
    84  					model.ErrUserFullNameTooLong,
    85  					model.ErrUserPasswordEmpty,
    86  					model.ErrRoleUnknown,
    87  				}},
    88  			}),
    89  
    90  			Entry("invalid params", createUserParamsCase{
    91  				params: model.CreateUserParams{},
    92  				err: &multierror.Error{Errors: []error{
    93  					model.ErrUserNameEmpty,
    94  					model.ErrUserPasswordEmpty,
    95  					model.ErrRoleUnknown,
    96  				}},
    97  			}),
    98  		)
    99  	})
   100  
   101  	Describe("UpdateUserParams", func() {
   102  		type updateUserParamsCase struct {
   103  			params model.UpdateUserParams
   104  			err    error
   105  		}
   106  
   107  		DescribeTable("UpdateUserParams cases",
   108  			func(c updateUserParamsCase) {
   109  				expectErrOrNil(c.params.Validate(), c.err)
   110  			},
   111  
   112  			Entry("empty params are valid", updateUserParamsCase{
   113  				params: model.UpdateUserParams{},
   114  			}),
   115  
   116  			Entry("valid params", updateUserParamsCase{
   117  				params: model.UpdateUserParams{
   118  					Name:     model.String("johndoe"),
   119  					Email:    model.String("john@example.com"),
   120  					FullName: model.String("John Doe"),
   121  					Password: model.String("qwerty")}.
   122  					SetIsDisabled(false).
   123  					SetRole(model.ReadOnlyRole),
   124  			}),
   125  
   126  			Entry("name is too long", updateUserParamsCase{
   127  				params: model.UpdateUserParams{
   128  					FullName: model.String(strings.Repeat("X", 256)),
   129  				},
   130  				err: model.ErrUserFullNameTooLong,
   131  			}),
   132  
   133  			Entry("invalid params", updateUserParamsCase{
   134  				params: model.UpdateUserParams{
   135  					Name:     model.String(""),
   136  					FullName: model.String(""),
   137  					Email:    model.String(""),
   138  					Password: model.String("")}.
   139  					SetRole(model.InvalidRole),
   140  				err: &multierror.Error{Errors: []error{
   141  					model.ErrUserNameEmpty,
   142  					model.ErrUserEmailInvalid,
   143  					model.ErrUserPasswordEmpty,
   144  					model.ErrRoleUnknown,
   145  				}},
   146  			}),
   147  		)
   148  	})
   149  
   150  	Describe("UpdateUserPasswordParams", func() {
   151  		type updateUserPasswordParamsCase struct {
   152  			params model.UpdateUserPasswordParams
   153  			err    error
   154  		}
   155  
   156  		DescribeTable("UpdateUserPasswordParams cases",
   157  			func(c updateUserPasswordParamsCase) {
   158  				expectErrOrNil(c.params.Validate(), c.err)
   159  			},
   160  
   161  			Entry("valid params", updateUserPasswordParamsCase{
   162  				params: model.UpdateUserPasswordParams{
   163  					OldPassword: "",
   164  					NewPassword: "qwerty",
   165  				},
   166  			}),
   167  
   168  			Entry("empty new password", updateUserPasswordParamsCase{
   169  				params: model.UpdateUserPasswordParams{},
   170  				err:    model.ErrUserPasswordEmpty,
   171  			}),
   172  		)
   173  	})
   174  })