Example #1
0
func TestUpdateUserAccountMissingSignature(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "GW")
    jsonobj.Set("email", "*****@*****.**")
    jsonobj.Set("address", "Pre-White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/" + gw.Id, bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    if resp.StatusCode != http.StatusUnauthorized {
        t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
    }
}
Example #2
0
func (p *SetPasswordRequestHandler) HandleInputHandlerAfterSetup(cxt SetPasswordContext) (int, http.Header, io.WriterTo) {
	errors := make(map[string][]error)
	var obj jsonhelper.JSONObject
	var err error
	authDS := p.authDS
	if user := cxt.User(); user != nil {
		var userPassword *dm.UserPassword
		if user != nil {
			userPassword = dm.NewUserPassword(user.Id, cxt.Password())
		} else {
			userPassword = dm.NewUserPassword("", cxt.Password())
		}
		userPassword.Validate(true, errors)
		if len(errors) == 0 {
			userPassword, err = authDS.StoreUserPassword(userPassword)
		}
		obj = jsonhelper.NewJSONObject()
		userObj, _ := jsonhelper.Marshal(user)
		obj.Set("user", userObj)
		obj.Set("type", "user")
		obj.Set("message", "password changed")
	} else {
		return apiutil.OutputErrorMessage(ERR_MUST_SPECIFY_USERNAME.Error(), time.Time{}, http.StatusBadRequest, nil)
	}
	if len(errors) > 0 {
		return apiutil.OutputErrorMessage("Value errors. See result", errors, http.StatusBadRequest, nil)
	}
	if err != nil {
		return apiutil.OutputErrorMessage(err.Error(), time.Time{}, http.StatusInternalServerError, nil)
	}
	cxt.SetResult(obj)
	return 0, nil, nil
}
Example #3
0
func TestUpdateUserAccountInvalidUserId(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1)
    accessKey := accessKeys[0]
    otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "Tom J")
    jsonobj.Set("email", "*****@*****.**")
    jsonobj.Set("address", "White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/sdflsjflsjfslf", bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    if resp.StatusCode != http.StatusNotFound {
        t.Error("Expected ", http.StatusNotFound, " status code but received ", resp.StatusCode)
    }
}
Example #4
0
func (p *ViewContactRequestHandler) HandleInputHandlerAfterSetup(cxt ViewContactContext) (int, http.Header, io.WriterTo) {
	obj := jsonhelper.NewJSONObject()
	contactObj, _ := jsonhelper.Marshal(cxt.Contact())
	obj.Set("contact", contactObj)
	obj.Set("type", "contact")
	cxt.SetResult(obj)
	return 0, nil, nil
}
Example #5
0
func TestUpdateUserAccount1(t *testing.T) {
    ds, wm := initializeUpdateUserAccountDS()
    gw, _ := ds.FindUserAccountByUsername("firstpresident")
    accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1000)
    if len(accessKeys) == 0 {
        t.Error("Expected to find at least one access key stored.")
    }
    accessKey := accessKeys[0]
    otherUser := gw
    anobj, _ := jsonhelper.Marshal(otherUser)
    jsonobj := anobj.(jsonhelper.JSONObject)
    jsonobj.Set("name", "GW")
    jsonobj.Set("email", "*****@*****.**")
    jsonobj.Set("address", "Pre-White House")
    otherUser = new(dm.User)
    otherUser.InitFromJSONObject(jsonobj)
    jsonbuf, _ := json.Marshal(jsonobj)
    req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/update/" + otherUser.Id, bytes.NewBuffer(jsonbuf))
    req.Header.Set("Content-Type", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept", webmachine.MIME_TYPE_JSON+"; charset=utf-8")
    req.Header.Set("Accept-Charset", "utf-8")
    req.Header.Set("Accept-Encoding", "identity")
    req.Header.Set("Accept-Language", "en-us")
    req.Header.Set("Connection", "close")
    apiutil.NewSigner(accessKey.Id, accessKey.PrivateKey).SignRequest(req, 0)
    reqbytes, _ := http.DumpRequest(req, true)
    t.Log("Request is:\n", string(reqbytes), "\n\n")
    resp := webmachine.NewMockResponseWriter(req)
    wm.ServeHTTP(resp, req)
    t.Log("Response is:\n", resp.String(), "\n\n")
    if resp.StatusCode != http.StatusOK {
        t.Error("Expected ", http.StatusOK, " status code but received ", resp.StatusCode)
    }
    if resp.Header().Get("Content-Type") != req.Header.Get("Accept") {
        t.Error("Expected Content-Type \"", req.Header.Get("Accept"), "\" but received ", resp.Header().Get("Content-Type"))
    }
    user := new(dm.User)
    obj := jsonhelper.NewJSONObject()
    err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
    user.InitFromJSONObject(obj.GetAsObject("result"))
    if err != nil {
        t.Error("Error while unmarshaling JSON: ", err.String())
    }
    if obj.GetAsString("status") != "success" {
        t.Error("Expected status = \"success\", but was \"", obj.GetAsString("status"), "\"")
    }
    if user.Name != otherUser.Name {
        t.Error("Expected name = \"", otherUser.Name, "\", but was ", user.Name)
    }
    if user.Username != otherUser.Username {
        t.Error("Expected username = \"", otherUser.Username, "\", but was ", user.Username)
    }
    if user.Email != otherUser.Email {
        t.Error("Expected email = \"", otherUser.Email, "\", but was ", user.Email)
    }
    if user.PhoneNumber != otherUser.PhoneNumber {
        t.Error("Expected phone_number = \"", otherUser.PhoneNumber, "\", but was ", user.PhoneNumber)
    }
    if user.Address != otherUser.Address {
        t.Error("Expected address = \"", otherUser.Address, "\", but was ", user.Address)
    }
    if user.Role != otherUser.Role {
        t.Error("Expected role = ", otherUser.Role, " but was ", user.Role)
    }
    if user.Id != otherUser.Id {
        t.Error("Expected id to be ", otherUser.Id, ", but was ", user.Id)
    }
    if theuser, err := ds.RetrieveUserAccountById(otherUser.Id); err != nil || theuser == nil {
        if theuser == nil {
            t.Error("Unable to find User account by id ", otherUser.Id)
        }
        if err != nil {
            t.Error("Error trying to find user account by id: ", err.String())
        }
    }
    if theuser, err := ds.FindUserAccountByUsername(otherUser.Username); err != nil || theuser == nil {
        if theuser == nil {
            t.Error("Unable to find User account by username ", otherUser.Username)
        }
        if err != nil {
            t.Error("Error trying to find user account by username: "******"Found ", len(theusers), " User accounts by email for ", otherUser.Email, " rather than 1: ", theusers)
        }
        if err != nil {
            t.Error("Error trying to find user accounts by email: ", err.String())
        }
    }
}