Example #1
0
func TestCreateUserAccount(t *testing.T) {
	ds := inmemory.NewInMemoryDataStore()
	wm := webmachine.NewWebMachine()
	wm.AddRouteHandler(account.NewCreateAccountRequestHandler(ds, ds))
	buf := bytes.NewBufferString(`{"role": 9999999999999999, "name": "George Washington", "username": "******", "email":"*****@*****.**", "phone_number": "+1-405-555-5555", "address": "Valley Forge"}`)
	oldUser := new(dm.User)
	json.Unmarshal(buf.Bytes(), &oldUser)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/account/user/create/", buf)
	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)
	reqb, _ := httputil.DumpRequest(req, true)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != 200 {
		t.Error("Expected 200 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").GetAsObject("user"))
	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 != oldUser.Name {
		t.Logf("Request was\n%s\n================\n", string(reqb))
		t.Log("Response is:\n", resp.String(), "\n\n")
		t.Error("Expected name = \"", oldUser.Name, "\", but was ", user.Name)
	}
	if user.Username != oldUser.Username {
		t.Error("Expected username = \"", oldUser.Username, "\", but was ", user.Username)
	}
	if user.Email != oldUser.Email {
		t.Error("Expected email = \"", oldUser.Email, "\", but was ", user.Email)
	}
	if user.PhoneNumber != oldUser.PhoneNumber {
		t.Error("Expected phone_number = \"", oldUser.PhoneNumber, "\", but was ", user.PhoneNumber)
	}
	if user.Address != oldUser.Address {
		t.Error("Expected address = \"", oldUser.Address, "\", but was ", user.Address)
	}
	if user.Role != dm.ROLE_STANDARD {
		t.Error("Expected role = ", dm.ROLE_STANDARD, " but was ", user.Role)
	}
	if user.Id == "" {
		t.Error("Expected id to be populated, but was empty")
	}
}
Example #2
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())
		}
	}
}