Beispiel #1
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)
    }
}
Beispiel #2
0
func TestAuthSetPasswordAdmin(t *testing.T) {
	ds, wm := initializeAuthUserAccountDS()
	user, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys) != 1 {
		t.Error("Expected to find 1 access key stored, but found", len(accessKeys))
	}
	accessKey := accessKeys[0]
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("password", "hi ho hi ho")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/set_password/", bytes.NewBuffer(jsonbuf))
	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, _ := httputil.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"))
	}
	obj := jsonhelper.NewJSONObject()
	err := json.Unmarshal(resp.Buffer.Bytes(), &obj)
	if err != nil {
		t.Error("Unable to unmarshal setPassword response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "success" {
		t.Error("Expected successful operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if message := result.GetAsString("message"); message != "password changed" {
		t.Error("Expected message == \"password changed\", but was \"", message, "\"")
	}
	user2 := result.GetAsObject("user")
	uid := user2.GetAsString("id")
	if uid != user.Id {
		t.Error("Expected user id of", user.Id, ", but was", uid)
	}
	accessKeys2, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys2) != 1 {
		t.Error("Expected to find one access key stored, but found", len(accessKeys))
	}
}
Beispiel #3
0
func TestViewUserAccountInvalidUserId(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	gw, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(gw.Id, nil, 1)
	accessKey := accessKeys[0]
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/sdflsjflsjfslf", nil)
	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)
	}
}
Beispiel #4
0
func TestViewUserAccountAsNonAdminForOtherUser(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	ja, _ := ds.FindUserAccountByUsername("thirdpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(ja.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	accessKey := accessKeys[0]
	otherUser, _ := ds.FindUserAccountByUsername("secondpresident")
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/"+otherUser.Id, nil)
	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.StatusForbidden {
		t.Error("Expected ", http.StatusForbidden, " status code but received ", resp.StatusCode)
	}
}
Beispiel #5
0
func TestViewUserAccount(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	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
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/"+otherUser.Id, nil)
	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.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.Error())
	}
	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.Error())
		}
	}
	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.Error())
		}
	}
}