示例#1
0
文件: auth_test.go 项目: teefax/tmail
func Test_authorized(t *testing.T) {
	var err error
	assert := assert.New(t)
	scope.Cfg = new(config.Config)
	scope.Log, err = logger.New(ioutil.Discard, false)
	assert.NoError(err)
	scope.Cfg.SetRestServerLogin("good")
	scope.Cfg.SetRestServerPasswd("good")

	// no Auth
	w := httptest.NewRecorder()
	r, _ := http.NewRequest("GET", "http://localhost/foobar", nil)
	assert.False(authorized(w, r))
	assert.Equal(w.Code, http.StatusUnauthorized)
	assert.Equal(w.Header().Get("WWW-Authenticate"), "Basic realm=tmail REST server")

	// bad auth
	r.SetBasicAuth("bad", "bad")
	assert.False(authorized(w, r))
	r.SetBasicAuth("good", "bad")
	assert.False(authorized(w, r))
	r.SetBasicAuth("bad", "good")
	assert.False(authorized(w, r))
	assert.Equal(w.Code, http.StatusUnauthorized)

	// good auth
	w = httptest.NewRecorder()
	r.SetBasicAuth("good", "good")
	assert.True(authorized(w, r))
	assert.Equal(w.Code, 200)
}
示例#2
0
func Test_log_init(t *testing.T) {
	var err error
	scope.Cfg = new(config.Config)
	scope.Log, err = logger.New(ioutil.Discard, false)
	assert.NoError(t, err)

}
示例#3
0
func Test_Logger(t *testing.T) {
	var err error
	assert := assert.New(t)
	buff := bytes.NewBufferString("")
	w := httptest.NewRecorder()
	r, err := http.NewRequest("GET", "http://localhost/foobar", nil)
	assert.NoError(err)
	scope.Log, err = logger.New(buff, false)
	assert.NoError(err)

	l := NewLogger()
	n := negroni.New()
	// replace log for testing
	n.Use(l)
	n.UseHandler(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
		rw.WriteHeader(http.StatusNotFound)
	}))

	n.ServeHTTP(w, r)
	assert.Equal(http.StatusNotFound, w.Code)
	assert.False(len(buff.Bytes()) == 0)
}
示例#4
0
func TestHandlerUSers(t *testing.T) {
	var err error
	login := "******"
	assert := assert.New(t)
	assert.NoError(scope.Init())
	scope.Log, err = logger.New(ioutil.Discard, false)
	assert.NoError(err)

	// drop table users
	assert.NoError(scope.DB.DropTableIfExists(&core.User{}).Error)
	assert.NoError(scope.DB.AutoMigrate(&core.User{}).Error)

	// Get all users should return empty json array
	w := httptest.NewRecorder()
	r, _ := http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")

	usersGetAll(w, r)

	b, _ := ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
	assert.Equal("[]", string(b))

	// Add user
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("POST", "http://localhost/foobar", bytes.NewBufferString(`{"passwd": "passwd", "authRelay": true, "haveMailbox": true, "mailboxQuota": "1G"}`))
	r.SetBasicAuth("admin", "admin")
	ps := httprouter.Params{
		httprouter.Param{"user", login},
	}
	httpcontext.Set(r, "params", ps)

	usersAdd(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(201, w.Code, string(b))

	// Get users; should return one user
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	usersGetAll(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
	assert.NotEqual("[]", string(b))
	u := core.User{}
	assert.NoError(json.NewDecoder(bytes.NewReader(b[1 : len(b)-1])).Decode(&u))
	assert.Equal(login, u.Login)
	assert.Equal(true, u.AuthRelay)
	assert.Equal(true, u.HaveMailbox)
	assert.Equal("1G", u.MailboxQuota)

	// Get One users; should return one user
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	ps = httprouter.Params{
		httprouter.Param{"user", login},
	}
	httpcontext.Set(r, "params", ps)
	usersGetOne(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
	assert.NotEqual("[]", string(b))
	u = core.User{}
	assert.NoError(json.NewDecoder(bytes.NewReader(b)).Decode(&u))
	assert.Equal(login, u.Login)
	assert.Equal(true, u.AuthRelay)
	assert.Equal(true, u.HaveMailbox)
	assert.Equal("1G", u.MailboxQuota)

	// Del user
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("DELETE", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	ps = httprouter.Params{
		httprouter.Param{"user", login},
	}
	httpcontext.Set(r, "params", ps)
	usersDel(w, r)
	assert.Equal(200, w.Code)
}
示例#5
0
func TestHandlerQueue(t *testing.T) {
	var err error
	assert := assert.New(t)
	assert.NoError(scope.Init())
	scope.Log, err = logger.New(ioutil.Discard, false)
	assert.NoError(err)

	// drop table queue
	assert.NoError(scope.DB.DropTableIfExists(&core.QMessage{}).Error)
	assert.NoError(scope.DB.AutoMigrate(&core.QMessage{}).Error)

	// Get all message in queue should return empty json array
	w := httptest.NewRecorder()
	r, _ := http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	queueGetMessages(w, r)
	b, _ := ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
	assert.Equal("[]", string(b))

	// Add message
	message := core.QMessage{
		Uuid:                "uuid",
		Key:                 "key",
		AddedAt:             time.Now(),
		Status:              2,
		DeliveryFailedCount: 0,
	}
	assert.NoError(scope.DB.Create(&message).Error)

	// Get all message
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	queueGetMessages(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
	assert.NotEqual("[]", string(b))
	m := core.QMessage{}
	assert.NoError(json.NewDecoder(bytes.NewReader(b[1 : len(b)-1])).Decode(&m))
	assert.Equal(m.Uuid, "uuid")

	// Get one
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	ps := httprouter.Params{
		httprouter.Param{"id", fmt.Sprintf("%d", m.Id)},
	}
	httpcontext.Set(r, "params", ps)
	queueGetMessage(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
	assert.NotEqual("[]", string(b))
	m = core.QMessage{}
	assert.NoError(json.NewDecoder(bytes.NewReader(b)).Decode(&m))
	assert.Equal(m.Uuid, "uuid")

	// discard
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	ps = httprouter.Params{
		httprouter.Param{"id", fmt.Sprintf("%d", m.Id)},
	}
	httpcontext.Set(r, "params", ps)
	queueDiscardMessage(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))

	// bounce
	w = httptest.NewRecorder()
	r, _ = http.NewRequest("GET", "http://localhost/foobar", nil)
	r.SetBasicAuth("admin", "admin")
	ps = httprouter.Params{
		httprouter.Param{"id", fmt.Sprintf("%d", m.Id)},
	}
	httpcontext.Set(r, "params", ps)
	queueBounceMessage(w, r)
	b, _ = ioutil.ReadAll(w.Body)
	assert.Equal(200, w.Code, string(b))
}