Skip to content

Commit

Permalink
added manager login/logout tests
Browse files Browse the repository at this point in the history
  • Loading branch information
林志宇 committed Mar 27, 2019
1 parent da4f009 commit f51ef11
Show file tree
Hide file tree
Showing 6 changed files with 376 additions and 71 deletions.
2 changes: 1 addition & 1 deletion cmd/manager-node/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ var rootCmd = &cobra.Command{
}()

if mock {
err := m.AddMockData(&manager.MockConfig{
err := m.AddMockData(manager.MockConfig{
Nodes: mockNodes,
MaxTpsPerNode: mockMaxTps,
MaxRoutesPerNode: mockMaxRoutes,
Expand Down
6 changes: 1 addition & 5 deletions pkg/manager/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type Config struct {
PK cipher.PubKey `json:"public_key"`
SK cipher.SecKey `json:"secret_key"`
DBPath string `json:"db_path"`
NameRegexp string `json:"username_regexp"` // regular expression for usernames (no check if empty). TODO
PassRegexp string `json:"password_regexp"` // regular expression for passwords (no check of empty). TODO
PassSaltLen int `json:"password_salt_len"` // Salt Len for password verification data.
Cookies CookieConfig `json:"cookies"`
Interfaces InterfaceConfig `json:"interfaces"`
Expand Down Expand Up @@ -64,8 +62,6 @@ func GenerateLocalConfig() Config {
}

func (c *Config) FillDefaults() {
c.NameRegexp = `^(admin)$`
c.PassRegexp = `((?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20})`
c.PassSaltLen = 16
c.Cookies.FillDefaults()
c.Interfaces.FillDefaults()
Expand Down Expand Up @@ -98,8 +94,8 @@ type CookieConfig struct {
}

func (c *CookieConfig) FillDefaults() {
c.Path = "/"
c.ExpiresDuration = time.Hour * 12
c.Path = "/"
c.Secure = true
c.HttpOnly = true
c.SameSite = http.SameSiteDefaultMode
Expand Down
53 changes: 28 additions & 25 deletions pkg/manager/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ type MockConfig struct {
}

// AddMockData adds mock data to Manager Node.
func (m *Node) AddMockData(config *MockConfig) error {
func (m *Node) AddMockData(config MockConfig) error {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < config.Nodes; i++ {
pk, client := node.NewMockRPCClient(r, config.MaxTpsPerNode, config.MaxRoutesPerNode)
Expand All @@ -94,37 +94,40 @@ func (m *Node) ServeHTTP(w http.ResponseWriter, r *http.Request) {
mux.Use(middleware.Timeout(time.Second * 30))
mux.Use(middleware.Logger)

mux.Route("/auth", func(r chi.Router) {
r.Post("/create-account", m.users.CreateAccount(m.c.PassSaltLen, m.c.PassRegexp, m.c.NameRegexp))
r.Post("/login", m.users.Login())
r.Post("/logout", m.users.Logout())
})

mux.Route("/api", func(r chi.Router) {
r.Use(m.users.Authorize)

r.Get("/user/info", m.users.UserInfo())
r.Post("/user/change-password", m.users.ChangePassword(m.c.PassSaltLen, m.c.PassRegexp))
r.Group(func(r chi.Router) {
r.Post("/create-account", m.users.CreateAccount(m.c.PassSaltLen))
r.Post("/login", m.users.Login())
r.Post("/logout", m.users.Logout())
})

r.Group(func(r chi.Router) {
r.Use(m.users.Authorize)

r.Get("/user", m.users.UserInfo())
r.Post("/change-password", m.users.ChangePassword(m.c.PassSaltLen))

r.Get("/nodes", m.getNodes())
r.Get("/nodes/{pk}", m.getNode())
r.Get("/nodes", m.getNodes())
r.Get("/nodes/{pk}", m.getNode())

r.Get("/nodes/{pk}/apps", m.getApps())
r.Get("/nodes/{pk}/apps/{app}", m.getApp())
r.Put("/nodes/{pk}/apps/{app}", m.putApp())
r.Get("/nodes/{pk}/apps", m.getApps())
r.Get("/nodes/{pk}/apps/{app}", m.getApp())
r.Put("/nodes/{pk}/apps/{app}", m.putApp())

r.Get("/nodes/{pk}/transport-types", m.getTransportTypes())
r.Get("/nodes/{pk}/transport-types", m.getTransportTypes())

r.Get("/nodes/{pk}/transports", m.getTransports())
r.Post("/nodes/{pk}/transports", m.postTransport())
r.Get("/nodes/{pk}/transports/{tid}", m.getTransport())
r.Delete("/nodes/{pk}/transports/{tid}", m.deleteTransport())
r.Get("/nodes/{pk}/transports", m.getTransports())
r.Post("/nodes/{pk}/transports", m.postTransport())
r.Get("/nodes/{pk}/transports/{tid}", m.getTransport())
r.Delete("/nodes/{pk}/transports/{tid}", m.deleteTransport())

r.Get("/nodes/{pk}/routes", m.getRoutes())
r.Post("/nodes/{pk}/routes", m.postRoute())
r.Get("/nodes/{pk}/routes/{rid}", m.getRoute())
r.Put("/nodes/{pk}/routes/{rid}", m.putRoute())
r.Delete("/nodes/{pk}/routes/{rid}", m.deleteRoute())
r.Get("/nodes/{pk}/routes", m.getRoutes())
r.Post("/nodes/{pk}/routes", m.postRoute())
r.Get("/nodes/{pk}/routes/{rid}", m.getRoute())
r.Put("/nodes/{pk}/routes/{rid}", m.putRoute())
r.Delete("/nodes/{pk}/routes/{rid}", m.deleteRoute())
})
})

mux.ServeHTTP(w, r)
Expand Down
Loading

0 comments on commit f51ef11

Please sign in to comment.