本文整理匯總了Golang中github.com/eluleci/dock/messages.Message類的典型用法代碼示例。如果您正苦於以下問題:Golang Message類的具體用法?Golang Message怎麽用?Golang Message使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
在下文中一共展示了Message類的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: makeValidGoogleRequest
func makeValidGoogleRequest() messages.RequestWrapper {
googleData := make(map[string]interface{})
googleData["id"] = "someid"
googleData["idToken"] = "someidtoken"
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["google"] = googleData
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
return requestWrapper
}
示例2: makeValidFacebookRequest
func makeValidFacebookRequest() messages.RequestWrapper {
facebookData := make(map[string]interface{})
facebookData["id"] = "facebookuserid"
facebookData["accessToken"] = "facebookaccesstoken"
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["facebook"] = facebookData
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
return requestWrapper
}
示例3: TestResetPassword
func TestResetPassword(t *testing.T) {
config.SystemConfig = config.Config{}
config.SystemConfig.ResetPassword = map[string]string{
"senderEmail": "[email protected]",
"senderEmailPassword": "emailadresspassword",
"smtpServer": "mail.rihtim.com",
"smtpPort": "25",
"mailSubject": "Reset password!",
"mailContentTemplate": "Your new password is %s.",
}
Convey("Should return internal server error", t, func() {
correctConfig := config.SystemConfig.ResetPassword
config.SystemConfig.ResetPassword = nil
var message messages.Message
message.Body = make(map[string]interface{})
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, err := HandleResetPassword(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
config.SystemConfig.ResetPassword = correctConfig
})
Convey("Should return bad request", t, func() {
var message messages.Message
message.Body = make(map[string]interface{})
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, err := HandleResetPassword(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
})
Convey("Should return not found", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
err = &utils.Error{http.StatusNotFound, "Account not found."}
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, err := HandleResetPassword(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusNotFound)
})
Convey("Should return the error from adapters.HandlePut", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
accountData = make(map[string]interface{})
accountData["_id"] = "564f1a28e63bce219e1cc745"
return
}
adapters.Update = func(collection string, id string, data map[string]interface{}) (response map[string]interface{}, hookBody map[string]interface{}, err *utils.Error) {
err = &utils.Error{http.StatusInternalServerError, "Some error happened."}
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, err := HandleResetPassword(requestWrapper, &adapters.MongoAdapter{})
So(err, ShouldNotBeNil)
})
Convey("Should call adapters.HandlePut with new password and correct user resource", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
accountData = make(map[string]interface{})
accountData["_id"] = "564f1a28e63bce219e1cc745"
return
}
var isResCorrect bool
var isPasswordProvided bool
adapters.Update = func(collection string, id string, data map[string]interface{}) (response map[string]interface{}, hookBody map[string]interface{}, err *utils.Error) {
isResCorrect = strings.EqualFold("users", collection) && strings.EqualFold("564f1a28e63bce219e1cc745", id)
isPasswordProvided = len(data["password"].(string)) > 0
//.........這裏部分代碼省略.........
示例4: TestHandleLogin
func TestHandleLogin(t *testing.T) {
Convey("Should return bad request", t, func() {
var called bool
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
called = true
return
}
var message messages.Message
message.Body = make(map[string]interface{})
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, err := HandleLogin(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
So(called, ShouldBeFalse)
})
Convey("Should login with email", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
accountData = make(map[string]interface{})
// hased of 'zuhaha'
accountData["password"] = "$2a$10$wqvcYHiRvoCy5ZUurNz9wuokDH1DyXjfd8k6Hk4DSJKui76gx1yrO"
accountData["_id"] = "564f1a28e63bce219e1cc745"
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
message.Body["password"] = "zuhaha"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
response, err := HandleLogin(requestWrapper, &adapters.MongoAdapter{})
So(err, ShouldBeNil)
So(response.Status, ShouldEqual, http.StatusOK)
})
Convey("Should login with username", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
accountData = make(map[string]interface{})
// hased of 'zuhaha'
accountData["password"] = "$2a$10$wqvcYHiRvoCy5ZUurNz9wuokDH1DyXjfd8k6Hk4DSJKui76gx1yrO"
accountData["_id"] = "564f1a28e63bce219e1cc745"
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["username"] = "someusername"
message.Body["password"] = "zuhaha"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
response, _ := HandleLogin(requestWrapper, &adapters.MongoAdapter{})
So(response.Status, ShouldEqual, http.StatusOK)
})
Convey("Should return password error", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
accountData = make(map[string]interface{})
// hased of 'zuhaha'
accountData["password"] = "$2a$10$wqvcYHiRvoCy5ZUurNz9wuokDH1DyXjfd8k6Hk4DSJKui76gx1yrO"
accountData["_id"] = "564f1a28e63bce219e1cc745"
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
message.Body["password"] = "notzuhaha"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
response, _ := HandleLogin(requestWrapper, &adapters.MongoAdapter{})
So(response.Status, ShouldEqual, http.StatusUnauthorized)
})
Convey("Should return error if account doesn't exist", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
err = &utils.Error{http.StatusNotFound, "Item not found."}
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
//.........這裏部分代碼省略.........
示例5: TestHandleSignUp
func TestHandleSignUp(t *testing.T) {
Convey("Should return bad request", t, func() {
var called bool
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
called = true
return
}
var message messages.Message
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
So(called, ShouldBeFalse)
})
Convey("Should return bad request for password", t, func() {
var called bool
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
called = true
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["username"] = "elgefe"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
So(called, ShouldBeFalse)
})
Convey("Should return conflict", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
accountData = make(map[string]interface{})
return
}
var called bool
generateToken = func(userId string, userData map[string]interface{}) (tokenString string, err *utils.Error) {
called = true
err = &utils.Error{http.StatusConflict, "Exists."}
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
message.Body["password"] = "apasswordimpossibletofind"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusConflict)
So(called, ShouldBeFalse)
})
Convey("Should return internal server error", t, func() {
getAccountData = func(requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (accountData map[string]interface{}, err *utils.Error) {
return
}
generateToken = func(userId string, userData map[string]interface{}) (tokenString string, err *utils.Error) {
err = &utils.Error{http.StatusInternalServerError, "Generating token failed."}
return
}
adapters.Create = func(collection string, data map[string]interface{}) (response map[string]interface{}, hookBody map[string]interface{}, err *utils.Error) {
response = make(map[string]interface{})
response["_id"] = "564f1a28e63bce219e1cc745"
return
}
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["email"] = "[email protected]"
message.Body["password"] = "apasswordimpossibletofind"
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
response, _, _ := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(response.Status, ShouldEqual, http.StatusInternalServerError)
})
//.........這裏部分代碼省略.........
示例6: TestGoogleRegistration
func TestGoogleRegistration(t *testing.T) {
// https needs to be changed to http for mock server to work
googleTokenVerificationEndpoint = "http://mock.google.com"
// setting configuration to default
config.SystemConfig = config.Config{}
config.SystemConfig.Google = map[string]string{
"clientId": "googleclientid",
}
Convey("Should fail creating account with Google when idToken is missing", t, func() {
googleData := make(map[string]interface{})
googleData["id"] = "someid"
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["google"] = googleData
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
})
Convey("Should fail creating account with Google when id is missing", t, func() {
googleData := make(map[string]interface{})
googleData["idToken"] = "someidtoken"
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["google"] = googleData
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
})
Convey("Should fail creating account with Google when server configuration is missing", t, func() {
// removing google configuration
correctConfig := config.SystemConfig
config.SystemConfig.Google = nil
requestWrapper := makeValidGoogleRequest()
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
// revert back the google configuration
config.SystemConfig = correctConfig
})
Convey("Should fail when getting response from Google fails", t, func() {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
defer mockServer.Close()
setDefaultServer(mockServer)
requestWrapper := makeValidGoogleRequest()
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("Should fail when parsing response from Google fails", t, func() {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintln(w, `{invalidjsonresponse`)
}))
defer mockServer.Close()
setDefaultServer(mockServer)
requestWrapper := makeValidGoogleRequest()
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("Should fail when Google's response is not as expected", t, func() {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
// the field 'aud' is removed from the response body
fmt.Fprintln(w, `{"iss": "accounts.google.com","at_hash": "pSLbt169EwtjApOMQMoTnA","sub": "107419746647224140307","email_verified": "true","azp": "407408718192.apps.googleusercontent.com","hd": "miwi.com","email": "[email protected]","iat": "1449240861","exp": "1449244461","alg": "RS256","kid": "ce30d9f163852843c9a94ce1c1d711e4464d4391"}`)
}))
defer mockServer.Close()
setDefaultServer(mockServer)
//.........這裏部分代碼省略.........
示例7: TestFacebookRegistration
func TestFacebookRegistration(t *testing.T) {
// https needs to be changed to http for mock server to work
facebookTokenVerificationEndpoint = "http://graph.facebook.com/debug_token"
config.SystemConfig = config.Config{}
config.SystemConfig.Facebook = map[string]string{
"appId": "facebookappid",
"appToken": "facebookapptoken",
}
Convey("Should fail creating account with Facebook when id is missing", t, func() {
facebookData := make(map[string]interface{})
facebookData["accessToken"] = "facebookaccesstoken"
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["facebook"] = facebookData
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
})
Convey("Should fail creating account with Facebook when access token is missing", t, func() {
facebookData := make(map[string]interface{})
facebookData["id"] = "facebookuserid"
var message messages.Message
message.Body = make(map[string]interface{})
message.Body["facebook"] = facebookData
var requestWrapper messages.RequestWrapper
requestWrapper.Message = message
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusBadRequest)
})
Convey("Should fail creating account with Facebook when server configuration is missing", t, func() {
// removing google configuration
originalConfig := config.SystemConfig
config.SystemConfig.Facebook = nil
requestWrapper := makeValidFacebookRequest()
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
// revert back the original configuration
config.SystemConfig = originalConfig
})
Convey("Should fail when getting response from Facebook fails", t, func() {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
}))
defer mockServer.Close()
setDefaultServer(mockServer)
requestWrapper := makeValidFacebookRequest()
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("Should fail when parsing response from Facebook fails", t, func() {
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintln(w, `{invalidjsonresponse`)
}))
defer mockServer.Close()
setDefaultServer(mockServer)
requestWrapper := makeValidFacebookRequest()
_, _, err := HandleSignUp(requestWrapper, &adapters.MongoAdapter{})
So(err.Code, ShouldEqual, http.StatusInternalServerError)
})
Convey("Should fail when Facebook's response is not as expected", t, func() {
requestWrapper := makeValidFacebookRequest()
// when 'data' is not at root
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
fmt.Fprintln(w, `{}`)
}))
defer mockServer.Close()
setDefaultServer(mockServer)
//.........這裏部分代碼省略.........
示例8: TestHandleRequest
func TestHandleRequest(t *testing.T) {
hooks.ExecuteTrigger = func(className, when, method string,
parameters map[string][]string, body map[string]interface{}, multipart *multipart.Form,
user interface{}) (responseBody map[string]interface{}, err *utils.Error) {
return
}
isGrantedFuncThatReturnsTrue := func(collection string, requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (isGranted bool, user map[string]interface{}, err *utils.Error) {
isGranted = true
return
}
isGrantedFuncThatReturnsFalse := func(collection string, requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (isGranted bool, user map[string]interface{}, err *utils.Error) {
isGranted = false
return
}
resetFunctions()
Convey("Should call auth.GetPermissions", t, func() {
var called bool
auth.IsGranted = func(collection string, requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (isGranted bool, user map[string]interface{}, err *utils.Error) {
called = true
return
}
actor := &Actor{}
actor.class = "someclass"
handleRequest(actor, messages.RequestWrapper{})
So(called, ShouldBeTrue)
})
Convey("Should return permission error", t, func() {
auth.IsGranted = func(collection string, requestWrapper messages.RequestWrapper, dbAdapter *adapters.MongoAdapter) (isGranted bool, user map[string]interface{}, err *utils.Error) {
err = &utils.Error{http.StatusInternalServerError, ""}
return
}
actor := &Actor{}
actor.class = "someclass"
response := handleRequest(actor, messages.RequestWrapper{})
So(response.Status, ShouldEqual, http.StatusInternalServerError)
})
/////////////////////////
// GET
/////////////////////////
resetFunctions()
Convey("Should call handleGet", t, func() {
auth.IsGranted = isGrantedFuncThatReturnsTrue
var called bool
handleGet = func(a *Actor, requestWrapper messages.RequestWrapper) (response messages.Message, err *utils.Error) {
called = true
return
}
var m messages.Message
m.Command = "get"
var rw messages.RequestWrapper
rw.Message = m
actor := &Actor{}
actor.class = "someclass"
handleRequest(actor, rw)
So(called, ShouldBeTrue)
})
Convey("Should return Authorization error for GET", t, func() {
auth.IsGranted = isGrantedFuncThatReturnsFalse
var m messages.Message
m.Command = "get"
var rw messages.RequestWrapper
rw.Message = m
actor := &Actor{}
actor.class = "someclass"
response := handleRequest(actor, rw)
So(response.Status, ShouldEqual, http.StatusUnauthorized)
})
/////////////////////////
// POST
/////////////////////////
Convey("Should call handlePost", t, func() {
auth.IsGranted = isGrantedFuncThatReturnsTrue
var called bool
handlePost = func(a *Actor, requestWrapper messages.RequestWrapper, user interface{}) (response messages.Message, hookBody map[string]interface{}, err *utils.Error) {
called = true
return
//.........這裏部分代碼省略.........