Beispiel #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)
    }
}
Beispiel #2
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 #3
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")
	}
}
Beispiel #4
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 #5
0
func TestAuthLoginNoPassword(t *testing.T) {
	ds, wm := initializeAuthUserAccountDS()
	user, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("username", user.Username)
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", 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")
	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.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " 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 login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "error" {
		t.Error("Expected error operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if result.Len() != 1 {
		t.Error("Expected a result object with 1 entry, but has", result.Len(), "entries as:", result)
	}
	if password := result.GetAsArray("password"); len(password) != 1 || password[0] != auth.ERR_MUST_SPECIFY_PASSWORD.Error() {
		t.Error("Expected one error for missing password, but was", result)
	}
	if message := obj.GetAsString("message"); message != auth.ERR_VALUE_ERRORS.Error() {
		t.Error("Expected ERR_VALUE_ERRORS for message, but was", message)
	}
	if accessKeys2, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000); len(accessKeys2) != 1 {
		t.Error("Expected 1 access key after logging in, but found", len(accessKeys2))
	}
}
Beispiel #6
0
func TestViewUserAccountMissingSignature(t *testing.T) {
	ds, wm := initializeViewUserAccountDS()
	gw, _ := ds.FindUserAccountByUsername("firstpresident")
	req, _ := http.NewRequest(webmachine.GET, "http://localhost/api/v1/json/account/user/view/"+gw.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")
	resp := webmachine.NewMockResponseWriter(req)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != http.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " status code but received ", resp.StatusCode)
	}
}
Beispiel #7
0
func TestCreateUserAccountMissingSeveralFields(t *testing.T) {
	ds := inmemory.NewInMemoryDataStore()
	wm := webmachine.NewWebMachine()
	wm.AddRouteHandler(account.NewCreateAccountRequestHandler(ds, ds))
	buf := bytes.NewBufferString(`{"role": 9999999999999999, "name": "    ", "username": "******", "email": "hi ho hi ho", "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)
	wm.ServeHTTP(resp, req)
	if resp.StatusCode != 400 {
		t.Error("Expected 400 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("Error while unmarshaling JSON: ", err.String())
	}
	if obj.GetAsString("status") != "error" {
		t.Error("Expected status = \"error\", but was \"", obj.GetAsString("status"), "\"")
	}
	result := obj.GetAsObject("result")
	if result == nil {
		t.Error("Expected result != nil, but was nil")
	} else {
		if result.GetAsArray("name").Len() == 0 {
			t.Error("Expected an error on attribute \"name\", but was not found")
		}
		if result.GetAsArray("username").Len() == 0 {
			t.Error("Expected an error on attribute \"username\", but was not found")
		}
		if result.GetAsArray("email").Len() == 0 {
			t.Error("Expected an error on attribute \"email\", but was not found")
		}
	}
}
Beispiel #8
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 #9
0
func TestAuthLoginNoUsername(t *testing.T) {
	_, wm := initializeAuthUserAccountDS()
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("password", "number two")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", 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")
	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.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " 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 login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "error" {
		t.Error("Expected error operation, but had status:", status)
	}
	result := obj.GetAsObject("result")
	if result.Len() != 1 {
		t.Error("Expected a result object with 1 entry, but has", result.Len(), "entries as:", result)
	}
	if username := result.GetAsArray("username"); len(username) != 1 || username[0] != auth.ERR_MUST_SPECIFY_USERNAME.Error() {
		t.Error("Expected one error for missing username, but was", result)
	}
	if message := obj.GetAsString("message"); message != auth.ERR_VALUE_ERRORS.Error() {
		t.Error("Expected ERR_VALUE_ERRORS for message, but was", message)
	}
}
Beispiel #10
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 #11
0
func TestAuthLoginAccountDoesNotExist(t *testing.T) {
	_, wm := initializeAuthUserAccountDS()
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("username", "dudewhatever")
	jsonobj.Set("password", "blah blah")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", 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")
	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.StatusUnauthorized {
		t.Error("Expected ", http.StatusUnauthorized, " 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 login response due to error:", err.Error())
	}
	if status := obj.GetAsString("status"); status != "error" {
		t.Error("Expected error operation, but had status:", status)
	}
	if result := obj.Get("result"); result != nil {
		t.Error("Expected result to be nil, but was", result)
	}
	if message := obj.GetAsString("message"); message != auth.ERR_INVALID_USERNAME_PASSWORD_COMBO.Error() {
		t.Error("Expected ERR_INVALID_USERNAME_PASSWORD_COMBO for message, but was", message)
	}
}
Beispiel #12
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())
		}
	}
}
Beispiel #13
0
func TestAuthLoginAdmin(t *testing.T) {
	ds, wm := initializeAuthUserAccountDS()
	user, _ := ds.FindUserAccountByUsername("firstpresident")
	accessKeys, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
	if len(accessKeys) == 0 {
		t.Error("Expected to find at least one access key stored.")
	}
	accessKey := accessKeys[0]
	jsonobj := jsonhelper.NewJSONObject()
	jsonobj.Set("username", user.Username)
	jsonobj.Set("password", "number one")
	jsonbuf, _ := json.Marshal(jsonobj)
	req, _ := http.NewRequest(webmachine.POST, "http://localhost/api/v1/json/auth/login/", 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")
	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 login 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 result == nil {
		t.Error("Expected an object for result, but was nil")
	} else {
		accessKeys2, _, _ := ds.RetrieveUserKeys(user.Id, nil, 1000)
		if len(accessKeys2) != 2 {
			t.Error("Expected 2 access keys after logging in, but found", len(accessKeys2))
		} else {
			var checkAccessKey *dm.AccessKey
			if accessKeys2[0].Id == accessKey.Id {
				checkAccessKey = accessKeys2[1]
			} else {
				checkAccessKey = accessKeys2[0]
			}
			if access_key_id := result.GetAsString("access_key_id"); access_key_id != checkAccessKey.Id {
				t.Error("Expected access_key_id with value", checkAccessKey.Id, "but was", access_key_id)
			}
			if private_key := result.GetAsString("private_key"); private_key != checkAccessKey.PrivateKey {
				t.Error("Expected private_key with value", checkAccessKey.PrivateKey, "but was", private_key)
			}
		}
		if username := result.GetAsString("username"); username != user.Username {
			t.Error("Expected username", user.Username, "but was", username)
		}
		if name := result.GetAsString("name"); name != user.Name {
			t.Error("Expected name", user.Name, "but was", name)
		}
		if user_id := result.GetAsString("user_id"); user_id != user.Id {
			t.Error("Expected user_id", user.Id, "but was", user_id)
		}
	}
}