func TestHandlerTest(t *testing.T) { cfg, err := conf.Parse(test.TcConfigName()) if err != nil { t.Fatalf("invalid behavior") } err = cfg.Validate() if err != nil { t.Fatalf("invalid behavior") } ctx, cancel := context.WithCancel(conf.NewContext(cfg)) defer cancel() r := &http.Request{} ctx, _ = auth.CheckToken(ctx, r, false) s, err := db.NewSession(cfg.Conn, true) if err != nil { t.Fatal(err) } defer s.Close() ctx = db.NewContext(ctx, s) ctx, err = auth.Authenticate(ctx) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() if err := HandlerTest(ctx, w, r); err.Err != nil { t.Error(err.Err) } if w.Code != http.StatusOK { t.Error("invalid behavior") } }
func TestNewContext(t *testing.T) { cfg, err := Parse(test.TcConfigName()) if err != nil { t.Fatalf("invalid") } if cfg == nil { t.Errorf("incorrect behavior") } defer cfg.Close() ctx := NewContext(cfg) if cfg2, err := FromContext(ctx); err != nil || cfg2 != cfg { t.Errorf("incorrect behavior") } }
func BenchmarkContext(b *testing.B) { cfg, err := conf.Parse(test.TcConfigName()) if err != nil { b.Fatal("invalid behavior") } err = cfg.Validate() if err != nil { b.Fatal("invalid behavior") } ctx := conf.NewContext(cfg) s, err := NewSession(cfg.Conn, false) if err != nil { b.Fatal(err) } defer s.Close() ctx = NewContext(ctx, s) b.ResetTimer() for i := 0; i < b.N; i++ { if _, err := C(ctx, "test"); err != nil { b.Error(err) } } }
func BenchmarkSession(b *testing.B) { cfg, err := conf.Parse(test.TcConfigName()) if err != nil { b.Fatal("invalid behavior") } err = cfg.Validate() if err != nil { b.Fatal("invalid behavior") } s, err := NewSession(cfg.Conn, true) if err != nil { b.Fatal(err) } s.Close() b.ResetTimer() for i := 0; i < b.N; i++ { s, err := NewSession(cfg.Conn, true) if err != nil { b.Error(err) } s.Close() } }
func TestNewSession(t *testing.T) { cfg, err := conf.Parse(test.TcConfigName()) if err != nil { t.Fatalf("invalid behavior") } err = cfg.Validate() if err != nil { t.Fatalf("invalid behavior") } s, err := NewSession(cfg.Conn, true) if err != nil { t.Fatalf("invalid db init") } defer s.Close() ctx := conf.NewContext(cfg) if _, err := CtxSession(ctx); err == nil { t.Fatalf("invalid behavior") } if _, err := C(ctx, "tests"); err == nil { t.Fatalf("invalid behavior") } ctx = NewContext(ctx, s) if _, err := CtxSession(ctx); err != nil { t.Error(err) } if _, err := C(ctx, "test-bad"); err == nil { t.Fatalf("invalid behavior") } coll, err := C(ctx, "tests") if err != nil { t.Error(err) } _, err = coll.Count() if err != nil { t.Error(err) } }
func TestParseConfig(t *testing.T) { name := "bad" cfg, err := Parse(name) if err == nil { t.Fatal("incorrect behavior") } name = test.TcConfigName() cfg, err = Parse(name + " ") if err != nil { t.Fatalf("invalid [%v]: %v", name, err) } if cfg == nil { t.Errorf("incorrect behavior") } // check mongo addresses if len(cfg.Db.Addrs()) == 0 { t.Errorf("incorrect behavior") } if u := cfg.Address(""); !strings.HasPrefix(u, "http") { t.Errorf("incorrect behavior") } // validate parameters oldDomain := cfg.Domain.Name cfg.Domain.Name = "" if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Domain.Name = oldDomain oldPort := cfg.Listener.Port cfg.Listener.Port = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Listener.Port = oldPort oldTimeout := cfg.Listener.Timeout cfg.Listener.Timeout = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Listener.Timeout = oldTimeout oldSecurityTokenLen := cfg.Listener.Security.TokenLen cfg.Listener.Security.TokenLen = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Listener.Security.TokenLen = oldSecurityTokenLen oldSecuritySalt := cfg.Listener.Security.Salt cfg.Listener.Security.Salt = "abc" if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Listener.Security.Salt = oldSecuritySalt oldMaxSpam := cfg.Settings.MaxSpam cfg.Settings.MaxSpam = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.MaxSpam = oldMaxSpam oldCleanMin := cfg.Settings.CleanMin cfg.Settings.CleanMin = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.CleanMin = oldCleanMin oldCbNum := cfg.Settings.CbNum cfg.Settings.CbNum = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.CbNum = oldCbNum oldCbBuf := cfg.Settings.CbBuf cfg.Settings.CbBuf = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.CbBuf = oldCbBuf oldCbLength := cfg.Settings.CbLength cfg.Settings.CbLength = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.CbLength = oldCbLength oldMaxName := cfg.Settings.MaxName cfg.Settings.MaxName = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.MaxName = oldMaxName oldMaxPack := cfg.Settings.MaxPack cfg.Settings.MaxPack = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.MaxPack = oldMaxPack oldMaxReqSize := cfg.Settings.MaxReqSize cfg.Settings.MaxReqSize = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.MaxReqSize = oldMaxReqSize oldTrackers := cfg.Settings.Trackers cfg.Settings.Trackers = 0 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Settings.Trackers = oldTrackers oldCacheURLs := cfg.Cache.URLs cfg.Cache.URLs = -1 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Cache.URLs = oldCacheURLs oldCacheTemplates := cfg.Cache.Templates cfg.Cache.Templates = -1 if err := cfg.Validate(); err == nil { t.Errorf("incorrect behavior") } cfg.Cache.Templates = oldCacheTemplates cfg, err = Parse(name) if err != nil { t.Fatal("incorrect behavior") } if err := cfg.Validate(); err != nil { t.Errorf("incorrect behavior: %v", err) } }
func TestToken(t *testing.T) { cfg, err := conf.Parse(test.TcConfigName()) if err != nil { t.Fatalf("invalid behavior") } err = cfg.Validate() if err != nil { t.Fatalf("invalid behavior") } ctx, cancel := context.WithCancel(conf.NewContext(cfg)) defer cancel() // check form auth r := &http.Request{PostForm: url.Values{"token": {""}}} if _, err := CheckToken(ctx, r, false); err != ErrAnonymous { t.Error("invalid behavior") } r = &http.Request{PostForm: url.Values{"token": {"bad"}}} if _, err := CheckToken(ctx, r, false); err == nil || err == ErrAnonymous { t.Error("invalid behavior") } p1, p2, err := genToken(cfg) if err != nil { t.Fatal(err) } r = &http.Request{PostForm: url.Values{"token": {p1 + p2}}} ctxToken, err := CheckToken(ctx, r, false) if err != nil { t.Errorf("invalid behavior: %v", err) } strToken, err := ExtractTokenKey(ctxToken) if err != nil { t.Errorf("invalid behavior: %v", err) } if p2 != strToken { t.Errorf("invalid behavior: %v != %v", p2, strToken) } // check API auth r = &http.Request{} if _, err := CheckToken(ctx, r, true); err != ErrAnonymous { t.Error("invalid behavior") } r = &http.Request{Header: http.Header{}} r.Header.Set("Authorization", "bad") if _, err := CheckToken(ctx, r, true); err == nil { t.Error("invalid behavior") } r = &http.Request{Header: http.Header{}} r.Header.Set("Authorization", "Bearer") if _, err := CheckToken(ctx, r, true); err == nil { t.Error("invalid behavior") } r = &http.Request{Header: http.Header{}} r.Header.Set("Authorization", "Bearer1234") if _, err := CheckToken(ctx, r, true); err == nil { t.Error("invalid behavior") } r = &http.Request{Header: http.Header{}} r.Header.Set("Authorization", "Bearer"+p1+p2) if _, err := CheckToken(ctx, r, true); err != nil { t.Errorf("invalid behavior: %v", err) } }
func TestCreateUser(t *testing.T) { const userName = "******" cfg, err := conf.Parse(test.TcConfigName()) if err != nil { t.Fatalf("invalid behavior") } err = cfg.Validate() if err != nil { t.Fatalf("invalid behavior") } ctx, cancel := context.WithCancel(conf.NewContext(cfg)) defer cancel() s, err := db.NewSession(cfg.Conn, true) if err != nil { t.Fatal(err) } defer s.Close() ctx = db.NewContext(ctx, s) coll, err := db.Coll(s, "users") if err != nil { t.Fatal(err) } _, err = coll.RemoveAll(nil) if err != nil { t.Fatal(err) } err = InitUsers(cfg) if err != nil { t.Fatal(err) } if n, err := coll.Count(); err != nil || n != 2 { t.Errorf("n=%v, err=%v", n, err) } users, err := CreateUsers(ctx, []string{userName}) if err != nil { t.Fatal(err) } if users, err := CreateUsers(ctx, []string{userName}); err == nil { if users[0].Err == "" { t.Error("invalid behavior") } } r := &http.Request{PostForm: url.Values{"token": {users[0].T}}} ctx, err = CheckToken(ctx, r, false) if err != nil { t.Errorf("invalid behavior: %v", err) } ctx, err = Authenticate(ctx) if err != nil { t.Fatal(err) } u, err := ExtractUser(ctx) if err != nil { t.Fatal(err) } if u.String() != userName { t.Error("invalid behavior") } if !u.HasRole("user") { t.Error("invalid behavior") } if u.IsAnonymous() { t.Error("invalid behavior") } _, err = ChangeUsers(ctx, []string{userName}) if err != nil { t.Fatal(err) } if result, err := ChangeUsers(ctx, []string{"bad"}); err != nil { if result[0].Err == "" { t.Error("invalid behavior") } } _, err = DisableUsers(ctx, []string{userName}) if err != nil { t.Fatal(err) } if result, err := DisableUsers(ctx, []string{"bad"}); err != nil { if result[0].Err == "" { t.Error("invalid behavior") } } }