Exemplo n.º 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")
	}
}
Exemplo n.º 2
0
func (p *InMemoryDataStore) CreateUserAccount(user *dm.User) (*dm.User, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		user.Id = p.generateIdForAccount(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME)
	}
	if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_ID
	}
	if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_USERNAME
	}
	if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email); ok {
		return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_EMAIL
	}
	if err := user.BeforeCreate(); err != nil {
		return user, err
	}
	if err := user.BeforeSave(); err != nil {
		return user, err
	}
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id, user)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username, user.Id)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email, user.Id)
	if err := user.AfterSave(); err != nil {
		return user, err
	}
	if err := user.AfterCreate(); err != nil {
		return user, err
	}
	return user, nil
}
Exemplo n.º 3
0
func (p *InMemoryDataStore) DeleteUserAccount(user *dm.User) (*dm.User, error) {
	if user == nil {
		return nil, nil
	}
	oldUserI, _ := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id)
	var oldUser *dm.User = nil
	if oldUserI != nil {
		oldUser = oldUserI.(*dm.User)
	}
	if err := user.BeforeDelete(); err != nil {
		return user, err
	}
	if oldUser != nil {
		p.delete(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, oldUser.Id)
		p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, oldUser.Username)
		p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, oldUser.Email)
	}
	if err := user.AfterDelete(); err != nil {
		return user, err
	}
	return oldUser, nil
}
Exemplo n.º 4
0
func (p *InMemoryDataStore) UpdateUserAccount(user *dm.User) (*dm.User, error) {
	if user == nil {
		return nil, nil
	}
	if user.Id == "" {
		return user, dm.ERR_MUST_SPECIFY_ID
	}
	oldUserI, _ := p.retrieve(_INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id)
	var oldUser *dm.User = nil
	if oldUserI != nil {
		oldUser = oldUserI.(*dm.User)
	}
	if oldUser == nil || oldUser.Username != user.Username {
		if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username); ok {
			return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_USERNAME
		}
	}
	if oldUser == nil || oldUser.Email != user.Email {
		if _, ok := p.retrieve(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email); ok {
			return user, ba.ERR_ACCOUNT_ALREADY_EXISTS_WITH_SPECIFIED_EMAIL
		}
	}
	if err := user.BeforeUpdate(); err != nil {
		return user, err
	}
	if err := user.BeforeSave(); err != nil {
		return user, err
	}
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_COLLECTION_NAME, user.Id, user)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, user.Username, user.Id)
	p.store(user.Id, _INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, user.Email, user.Id)
	if oldUser != nil {
		if oldUser.Username != user.Username {
			p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_USERNAME_COLLECTION_NAME, oldUser.Username)
		}
		if oldUser.Email != user.Email {
			p.delete(_INMEMORY_USER_ACCOUNT_ID_FOR_EMAIL_COLLECTION_NAME, oldUser.Email)
		}
	}
	if err := user.AfterSave(); err != nil {
		return user, err
	}
	if err := user.AfterUpdate(); err != nil {
		return user, err
	}
	return user, nil
}
Exemplo n.º 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())
		}
	}
}