Best Syzkaller code snippet using main.createTestConfig
service_test.go
Source:service_test.go
...10 st "ruslanlesko/brightonum/src/structs"11)12var mailer = MailerMock{}13func TestAuthService_InviteUser(t *testing.T) {14 var token = issueTestToken(user.ID, user.Username, createTestConfig().PrivKeyPath)15 var email = "bojack@horseman.com"16 var codeMatcher = func(code string) bool {17 return len(code) == 3218 }19 var userMatcher = func(u *st.User) bool {20 return u.Email == email && len(u.InviteCode) == 3221 }22 dao := dao.MockUserDao{}23 dao.On("GetByUsername", user.Username).Return(&user, nil)24 dao.On("Save", mock.MatchedBy(userMatcher)).Return(42)25 m := MailerMock{}26 m.On("SendInviteCode", email, mock.MatchedBy(codeMatcher)).Return(nil)27 s := AuthService{&m, &dao, createTestConfig()}28 err := s.InviteUser(email, token)29 assert.Nil(t, err)30 dao.AssertExpectations(t)31 m.AssertExpectations(t)32}33func TestAuthService_InviteUser_Forbidden(t *testing.T) {34 var token = issueTestToken(user.ID, user.Username, createTestConfig().PrivKeyPath)35 var email = "bojack@horseman.com"36 dao := dao.MockUserDao{}37 dao.On("GetByUsername", user.Username).Return(&st.User{ID: user.ID + 1}, nil)38 s := AuthService{&mailer, &dao, createTestConfig()}39 err := s.InviteUser(email, token)40 assert.Equal(t, st.AuthError{Msg: "Available only for admin", Status: 403}, err)41 dao.AssertExpectations(t)42}43func TestAuthService_CreateUser(t *testing.T) {44 var u = st.User{ID: -1, Username: "uname", FirstName: "test", LastName: "user", Email: "test@email.com", Password: "pwd"}45 dao := dao.MockUserDao{}46 dao.On("Save", &u).Return(1)47 dao.On("GetByUsername", u.Username).Return(nil, nil)48 s := AuthService{&mailer, &dao, createTestConfig()}49 err := s.CreateUser(&u)50 assert.Nil(t, err)51 dao.AssertExpectations(t)52}53func TestAuthService_CreateUser_DuplicateHandling(t *testing.T) {54 u := st.User{ID: -1, Username: "alle", FirstName: "Alle", LastName: "Alle", Email: "alle@alle.com", Password: "pwd"}55 dao := dao.MockUserDao{}56 dao.On("GetByUsername", u.Username).Return(&u, nil)57 s := AuthService{&mailer, &dao, createTestConfig()}58 err := s.CreateUser(&u)59 assert.Equal(t, st.AuthError{Msg: "Username already exists", Status: 400}, err)60}61func TestAuthService_BasicAuthToken(t *testing.T) {62 user := createTestUser()63 username := user.Username64 password := "oakheart"65 dao := dao.MockUserDao{}66 dao.On("GetByUsername", username).Return(&user, nil)67 s := AuthService{&mailer, &dao, createTestConfig()}68 accessToken, refreshToken, err := s.BasicAuthToken(username, password)69 assert.Nil(t, err)70 assert.NotEmpty(t, accessToken)71 assert.True(t, testJWTIntField(accessToken, "userId", 42))72 assert.True(t, testJWTStringField(accessToken, "sub", "alle"))73 assert.NotEmpty(t, refreshToken)74 assert.True(t, testJWTStringField(refreshToken, "sub", "alle"))75 expRaw := exctractField(accessToken, "exp", -1)76 exp := int64(expRaw.(float64))77 estimatedEx := time.Now().Add(time.Hour).UTC().Unix()78 assert.True(t, exp >= estimatedEx-1 && exp <= estimatedEx+1)79 expRaw = exctractField(refreshToken, "exp", -1)80 exp = int64(expRaw.(float64))81 estimatedEx = time.Now().AddDate(1, 0, 0).UTC().Unix()82 assert.True(t, exp >= estimatedEx-1 && exp <= estimatedEx+1)83 accessToken, refreshToken, err = s.BasicAuthToken(username, password+"xyz")84 assert.Empty(t, accessToken)85 assert.Empty(t, refreshToken)86 assert.Equal(t, st.AuthError{Msg: "Username or password is wrong", Status: 403}, err)87}88func TestAuthService_RefreshToken(t *testing.T) {89 user := createTestUser()90 username := user.Username91 password := "oakheart"92 dao := dao.MockUserDao{}93 dao.On("GetByUsername", username).Return(&user, nil)94 s := AuthService{&mailer, &dao, createTestConfig()}95 accessToken, refreshToken, err := s.BasicAuthToken(username, password)96 assert.Nil(t, err)97 assert.NotEmpty(t, accessToken)98 refreshedToken, err := s.RefreshToken(refreshToken)99 assert.Nil(t, err)100 assert.NotEmpty(t, refreshedToken)101 refreshedToken, err = s.RefreshToken(refreshedToken + "xyz")102 assert.Empty(t, refreshedToken)103 assert.Equal(t, st.AuthError{Msg: "Refresh token is not valid", Status: 403}, err)104}105func TestAuthService_GetUserByToken(t *testing.T) {106 user := createTestUser()107 username := user.Username108 password := "oakheart"109 dao := dao.MockUserDao{}110 dao.On("GetByUsername", username).Return(&user, nil)111 s := AuthService{&mailer, &dao, createTestConfig()}112 token, _, err := s.BasicAuthToken(username, password)113 assert.Nil(t, err)114 u, err := s.GetUserByToken(token)115 assert.Nil(t, err)116 assert.Equal(t, user, *u)117 u, err = s.GetUserByToken(token + "xyz")118 assert.NotNil(t, err)119 assert.Nil(t, u)120}121func TestAuthService_GetUsers(t *testing.T) {122 user1 := createTestUser()123 user2 := createAnotherTestUser()124 token := issueTestToken(user1.ID, user1.Username, createTestConfig().PrivKeyPath)125 dao := dao.MockUserDao{}126 dao.On("GetAll").Return(&[]st.User{user1, user2}, nil)127 dao.On("GetByUsername", user1.Username).Return(&user1, nil)128 s := AuthService{&mailer, &dao, createTestConfig()}129 userInfo := createTestUserInfo()130 userInfo2 := createAdditionalTestUserInfo()131 expected := &[]st.UserInfo{userInfo, userInfo2}132 us, err := s.GetUsers(token)133 assert.Nil(t, err)134 assert.Equal(t, expected, us)135}136func TestAuthService_UpdateUser(t *testing.T) {137 user := createTestUserUpdatePayload()138 token := issueTestToken(user.ID, user.Username, createTestConfig().PrivKeyPath)139 dao := dao.MockUserDao{}140 dao.On("Get", user.ID).Return(&user, nil)141 dao.On("GetByUsername", user.Username).Return(&user, nil)142 dao.On("Update", &user).Return(nil)143 s := AuthService{&mailer, &dao, createTestConfig()}144 err := s.UpdateUser(&user, token)145 assert.Nil(t, err)146}147func TestAuthService_UpdateUserInvalidToken(t *testing.T) {148 user := createTestUserUpdatePayload()149 token := "invalid token"150 dao := dao.MockUserDao{}151 s := AuthService{&mailer, &dao, createTestConfig()}152 err := s.UpdateUser(&user, token)153 assert.Equal(t, st.AuthError{Msg: "Invalid token", Status: 401}, err)154}155func TestAuthService_DeleteUser(t *testing.T) {156 user := createTestUser()157 token := issueTestToken(user.ID, user.Username, createTestConfig().PrivKeyPath)158 dao := dao.MockUserDao{}159 dao.On("GetByUsername", user.Username).Return(&user, nil)160 dao.On("DeleteById", user.ID).Return(nil)161 s := AuthService{&mailer, &dao, createTestConfig()}162 err := s.DeleteUser(user.ID, token)163 assert.Nil(t, err)164}165func TestAuthService_SendRecoveryEmail(t *testing.T) {166 user := createTestUser()167 codeMatcher := func(code string) bool {168 return len(code) == 6169 }170 dao := dao.MockUserDao{}171 mailer.On(172 "SendRecoveryCode",173 user.Email,174 mock.MatchedBy(codeMatcher)).Return(nil)175 dao.On(176 "SetRecoveryCode",177 user.ID,178 mock.MatchedBy(func(hashedCode string) bool { return hashedCode != "" })).Return(nil)179 dao.On("GetByUsername", user.Username).Return(&user, nil)180 s := AuthService{&mailer, &dao, createTestConfig()}181 err := s.SendRecoveryEmail(user.Username)182 assert.Nil(t, err)183}184func TestAuthService_ExchangeRecoveryCode(t *testing.T) {185 user := createTestUser()186 code := "267483"187 hashedCode := "$2a$04$c12NAkAi9nOxkYM5vO7eUur2fd9M23M4roKPbroOvNhsBVF0mOmS."188 dao := dao.MockUserDao{}189 dao.On("GetByUsername", user.Username).Return(&user, nil)190 dao.On("GetRecoveryCode", user.ID).Return(hashedCode, nil)191 dao.On(192 "SetResettingCode",193 user.ID,194 mock.MatchedBy(func(hashedResettingCode string) bool { return hashedResettingCode != "" })).Return(nil)195 s := AuthService{&mailer, &dao, createTestConfig()}196 resettingCode, err := s.ExchangeRecoveryCode(user.Username, code)197 assert.Nil(t, err)198 assert.True(t, len(resettingCode) == 10)199}200func TestAuthService_ResetPassword(t *testing.T) {201 user := createTestUser()202 code := "267483"203 hashedCode := "$2a$04$c12NAkAi9nOxkYM5vO7eUur2fd9M23M4roKPbroOvNhsBVF0mOmS."204 dao := dao.MockUserDao{}205 dao.On("GetByUsername", user.Username).Return(&user, nil)206 dao.On("GetResettingCode", user.ID).Return(hashedCode, nil)207 dao.On(208 "ResetPassword",209 user.ID,210 mock.MatchedBy(func(hashedPassword string) bool { return hashedPassword != "" })).Return(nil)211 s := AuthService{&mailer, &dao, createTestConfig()}212 err := s.ResetPassword(user.Username, code, "kek")213 assert.Nil(t, err)214}215func createTestUser() st.User {216 return st.User{ID: 42, Username: "alle", FirstName: "test", LastName: "user", Email: "test@email.com", Password: "$2a$04$Mhlu1.a4QchlVgGQFc/0N.qAw9tsXqm1OMwjJRaPRCWn47bpsRa4S"}217}218func createTestUserUpdatePayload() st.User {219 return st.User{ID: 42, Email: "changed@email.com"}220}221func createTestUserInfo() st.UserInfo {222 return st.UserInfo{ID: 42, Username: "alle", FirstName: "test", LastName: "user", Email: "test@email.com"}223}224func createAnotherTestUser() st.User {225 return st.User{ID: 43, Username: "alle2", FirstName: "test", LastName: "user", Email: "test@email.com", Password: "$2a$04$Mhlu1.a4QchlVgGQFc/0N.qAw9tsXqm1OMwjJRaPRCWn47bpsRa4S"}226}227func createAdditionalTestUserInfo() st.UserInfo {228 return st.UserInfo{ID: 43, Username: "alle2", FirstName: "test", LastName: "user", Email: "test@email.com"}229}230func createTestConfig() Config {231 return Config{PrivKeyPath: "../test_data/private.pem", PubKeyPath: "../test_data/public.pem", AdminID: user.ID}232}233func testJWTIntField(tokenStr string, fieldName string, fieldValue int) bool {234 value := exctractField(tokenStr, fieldName, -1)235 actualValue, ok := value.(float64)236 if ok {237 return int(actualValue) == fieldValue238 }239 return false240}241func testJWTStringField(tokenStr string, fieldName string, fieldValue string) bool {242 value := exctractField(tokenStr, fieldName, "")243 actualValue, ok := value.(string)244 if !ok {...
config_test.go
Source:config_test.go
...17 return config, err18 }19 return validateConfig(config)20}21func createTestConfig(t *testing.T, conf string) string {22 tmpfile, err := ioutil.TempFile("", "sequins-conf-test")23 if err != nil {24 t.Fatal(err)25 }26 _, err = tmpfile.WriteString(conf)27 if err != nil {28 t.Fatal(err)29 }30 err = tmpfile.Close()31 if err != nil {32 t.Fatal(err)33 }34 return tmpfile.Name()35}36func TestExampleConfig(t *testing.T) {37 config, err := loadAndValidateConfig("sequins.conf.example")38 require.NoError(t, err, "sequins.conf.example should exist and be valid")39 defaults := defaultConfig()40 defaults.Source = config.Source41 assert.Equal(t, defaults, config, "sequins.conf.example should eval to the default config")42}43// TestExampleConfigDefaults uncomments the defaults in sequins.conf.example,44// and then checks that against the actual defaults. It tries to skip any45// options annotated with "Unset by default".46func TestExampleConfigDefaults(t *testing.T) {47 raw, err := ioutil.ReadFile("sequins.conf.example")48 require.NoError(t, err, "sequins.conf.example should be readable")49 // Uncomment all the commented defaults, and make sure they're the actual50 // defaults. But we have to skip ones that we document as "Unset by default".51 replaced := commentedDefaultRegex.ReplaceAllFunc(raw, func(match []byte) []byte {52 if bytes.Index(match, []byte("Unset by default.")) == -1 {53 return append(commentedDefaultRegex.FindSubmatch(match)[1], '\n')54 } else {55 return nil56 }57 })58 t.Logf("---replaced config---\n%s\n---replaced config---", string(replaced))59 path := createTestConfig(t, string(replaced))60 config, err := loadAndValidateConfig(path)61 require.NoError(t, err, "the uncommented sequins.conf.example should exist and be valid")62 defaults := defaultConfig()63 defaults.Source = config.Source64 assert.Equal(t, defaults, config, "the uncommented sequins.conf.example should eval to the default config")65 os.Remove(path)66}67func TestSimpleConfig(t *testing.T) {68 path := createTestConfig(t, `69 source = "s3://foo/bar"70 require_success_file = true71 refresh_period = "1h"72 [zk]73 servers = ["zk:2181"]74 `)75 config, err := loadAndValidateConfig(path)76 require.NoError(t, err, "loading a basic config should work")77 assert.Equal(t, "s3://foo/bar", config.Source, "Source should be set")78 assert.Equal(t, true, config.RequireSuccessFile, "RequireSuccessFile should be set")79 assert.Equal(t, time.Hour, config.RefreshPeriod.Duration, "RefreshPeriod (a duration) should be set")80 assert.Equal(t, []string{"zk:2181"}, config.ZK.Servers, "ZK.Servers should be set")81 defaults := defaultConfig()82 defaults.Source = config.Source83 defaults.RequireSuccessFile = config.RequireSuccessFile84 defaults.RefreshPeriod = config.RefreshPeriod85 defaults.ZK.Servers = config.ZK.Servers86 assert.Equal(t, defaults, config, "the configuration should otherwise be the default")87 os.Remove(path)88}89func TestEmptyConfig(t *testing.T) {90 path := createTestConfig(t, "source = \"/foo\"")91 config, err := loadAndValidateConfig(path)92 require.NoError(t, err, "loading an empty config should work")93 assert.Equal(t, "/foo", config.Source, "the root should be set")94 config.Source = ""95 assert.Equal(t, defaultConfig(), config, "an empty config should eval to the default config")96}97func TestConfigSearchPath(t *testing.T) {98 path := createTestConfig(t, "source = \"/foo\"")99 _, err := loadAndValidateConfig(fmt.Sprintf("%s:/this/doesnt/exist.conf", path))100 assert.NoError(t, err, "it should find the config file on the search path")101 _, err = loadAndValidateConfig(fmt.Sprintf("/this/doesnt/exist.conf:%s", path))102 assert.NoError(t, err, "it should find the config file on the search path")103 os.Remove(path)104}105func TestConfigExtraKeys(t *testing.T) {106 path := createTestConfig(t, `107 source = "s3://foo/bar"108 require_success_file = true109 refresh_period = "1h"110 [zk]111 servers = ["zk:2181"]112 foo = "bar" # not a real config property!113 `)114 _, err := loadAndValidateConfig(path)115 assert.Error(t, err, "it should throw an error if there are extra config properties")116 os.Remove(path)117}118func TestConfigInvalidCompression(t *testing.T) {119 path := createTestConfig(t, `120 source = "s3://foo/bar"121 require_success_file = true122 refresh_period = "1h"123 [storage]124 compression = "notacompression"125 `)126 _, err := loadAndValidateConfig(path)127 assert.Error(t, err, "it should throw an error if an invalid compression is specified")128 os.Remove(path)129}130func TestConfigRelativeSource(t *testing.T) {131 path := createTestConfig(t, `132 source = "foo/bar"133 local_store = "/foo/bar/baz"134 `)135 _, err := loadAndValidateConfig(path)136 assert.Error(t, err, "it should throw an error if the source root is a relative path")137}138func TestConfigRelativeSourceURL(t *testing.T) {139 path := createTestConfig(t, `140 source = "file://foo/bar"141 local_store = "/foo/bar/baz"142 `)143 _, err := loadAndValidateConfig(path)144 assert.Error(t, err, "it should throw an error if the source root is a relative path")145}146func TestConfigRelativeLocalStore(t *testing.T) {147 path := createTestConfig(t, `148 source = "/foo/bar"149 local_store = "bar/baz"150 `)151 _, err := loadAndValidateConfig(path)152 assert.Error(t, err, "it should throw an error if the local store is a relative path")153}154func TestConfigLocalStoreWithinRoot(t *testing.T) {155 path := createTestConfig(t, `156 source = "/foo/bar"157 local_store = "/foo/bar/baz"158 `)159 _, err := loadAndValidateConfig(path)160 assert.Error(t, err, "it should throw an error if the local store is within the source root")161}...
createTestConfig
Using AI Code Generation
1import (2func TestCreateTestConfig(t *testing.T) {3 config := createTestConfig()4 fmt.Println(config)5}6import (7func TestCreateTestConfig(t *testing.T) {8 config := createTestConfig()9 fmt.Println(config)10}11I am trying to test the method createTestConfig() of main package from another package. I am getting the error: cannot refer to unexported name main.createTestConfig. How do I solve this issue?12I have a main package and a subpackage. I want to test the method createTestConfig() of main package from the subpackage. I am getting the error: cannot refer to unexported name main.createTestConfig. How do I solve this issue?13I am trying to test a method in a package. I have a main package and a subpackage. I want to test the method createTestConfig() of main package from the subpackage. I am getting the error: cannot refer to unexported name main.createTestConfig. How do I solve this issue?14I am trying to test a method in a package. I have a main package and a subpackage. I want to test the method createTestConfig() of main package from the subpackage. I am getting the error: cannot refer to unexported name main.createTestConfig. How do I solve this issue?15I am trying to test a method in a package. I have a main package and a subpackage. I want to test the method createTestConfig() of main package from the subpackage. I am getting the error: cannot refer to unexported name main.createTestConfig. How do I solve this issue?16I am trying to test a method in a package. I have a main package and a subpackage. I want to test the method createTestConfig() of main package from the subpackage. I am getting the error: cannot refer to unexported name main.createTestConfig. How do I solve this issue?17I am trying to test a method in a package. I have a main package and a subpackage. I want to test the method createTestConfig() of main package from the subpackage. I am getting the error: cannot refer to unexport
createTestConfig
Using AI Code Generation
1import (2func main() {3 fmt.Println("Hello World!")4}5import (6func main() {7 fmt.Println("Hello World!")8}
createTestConfig
Using AI Code Generation
1import (2func main() {3 config := createTestConfig()4 fmt.Println("config:", config)5}6config: {test 1234}7func foo() (string, string) {8}9func main() {10 s1, s2 := foo()11 fmt.Println(s1)12 fmt.Println(s2)13}14func main() {15 s := foo()16 fmt.Println(s)17}18func main() {19 _, s2 := foo()20 fmt.Println(s2)21}22func foo() (s1, s2 string) {23}24func foo() (s1, s2 string) {25}26func foo() (s1, s2 string) {
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!