Пример #1
0
func TestUserAvatars(t *testing.T) {
	setup()
	uam := new(UserAvatarManager)
	//Create the user Avatar Db
	err := services.CreateDb(services.AvatarDb)
	if err != nil {
		t.Error(err)
	}
	err = setAvatarDbSecurity()
	if err != nil {
		t.Error(err)
	}
	defer services.DeleteDb(services.AvatarDb)
	defer services.DeleteDb(services.MainDb)
	smithUser := func() *entities.CurrentUserInfo {
		smithAuth := &couchdb.BasicAuth{Username: "******", Password: "******"}
		smith, err := services.GetUserFromAuth(smithAuth)
		if err != nil {
			t.Error(err)
			return &entities.CurrentUserInfo{}
		}
		return &entities.CurrentUserInfo{
			Auth: smithAuth,
			User: smith,
		}
	}
	//after test cleanup
	defer func() {
		getSmithUser := func() *entities.CurrentUserInfo {
			return smithUser()
		}
		um.Delete("Steven.Smith", getSmithUser())
	}()
	//Register user, get things set up, etc.
	user := entities.User{
		UserName: "******",
		Password: "******",
	}
	registration := Registration{
		NewUser: user,
	}
	rev, err := um.SetUp(&registration)
	if err != nil {
		t.Error(err)
	} else {
		t.Logf("User Rev: %v", rev)
	}
	//Now, create a User Avatar Record
	uar := entities.UserAvatar{
		UserName: "******",
	}
	aRev, err := uam.Save("Steven.Smith", "", &uar, smithUser())
	if err != nil {
		t.Error(err)
	} else {
		t.Logf("User Avatar Rev: %v", aRev)
	}
	//Save the image
	t.Logf("Decoding Hex String")
	imageBytes, err := hex.DecodeString(tinyJpeg)
	if err != nil {
		t.Error(err)
	}
	t.Logf("Creating io.Reader and sending to UAM")
	imageReader := bytes.NewReader(imageBytes)
	iRev, err := uam.SaveImage("Steven.Smith", aRev, "image/jpeg",
		imageReader, smithUser())
	if err != nil {
		t.Error(err)
	} else {
		t.Logf("User Avatar Image Rev: %v", iRev)
	}
	//Read the Avatar Record
	readRecord := entities.UserAvatar{}
	rRev, err := uam.Read("Steven.Smith", &readRecord, smithUser())
	if err != nil {
		t.Error(err)
	}
	t.Logf("Read Avatar Record with Revision: %v", rRev)
	//Read the Avatar Large Image
	imgData, err := uam.GetLargeAvatar("Steven.Smith")
	if err != nil {
		t.Error(err)
	}
	imgBytes, err := ioutil.ReadAll(imgData)
	imgData.Close()
	if len(imgBytes) == 0 {
		t.Error("Image was zero length!")
	}
	t.Logf("Read Avatar Large Image with %v bytes", len(imgBytes))
	//Read the Avatar Thumbnail
	imgData, err = uam.GetThumbnailAvatar("Steven.Smith")
	if err != nil {
		t.Error(err)
	}
	imgBytes, err = ioutil.ReadAll(imgData)
	imgData.Close()
	if len(imgBytes) == 0 {
		t.Error("Image was zero length!")
	}
	t.Logf("Read Avatar Thumbnail Image with %v bytes", len(imgBytes))
	//Delete Avatar Record
	dRev, err := uam.Delete("Steven.Smith", smithUser())
	if err != nil {
		t.Error(err)
	}
	t.Logf("Delete Avatar with Rev: %v", dRev)
}
Пример #2
0
func TestUsers(t *testing.T) {
	setup()
	defer services.DeleteDb(services.MainDb)
	smithUser := func() *entities.CurrentUserInfo {
		smithAuth := &couchdb.BasicAuth{Username: "******", Password: "******"}
		smith, err := services.GetUserFromAuth(smithAuth)
		if err != nil {
			t.Error(err)
			return &entities.CurrentUserInfo{}
		}
		return &entities.CurrentUserInfo{
			Auth: smithAuth,
			User: smith,
		}
	}
	//after test cleanup
	defer func() {
		getSmithUser := func() *entities.CurrentUserInfo {
			return smithUser()
		}
		um.Delete("Steven.Smith", getSmithUser())
	}()
	Convey("Given a new account registration", t, func() {
		user := entities.User{
			UserName: "******",
			Password: "******",
		}
		registration := user_service.Registration{
			NewUser: user,
		}
		Convey("When the new user is registered", func() {
			rev, err := um.SetUp(&registration)
			Convey("The revision should be set and the error should be nil", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
				t.Logf("New user revision: %v", rev)
			})
		})
		Convey("When a new user for the account is created", func() {
			subUser := entities.User{
				UserName: "******",
				Password: "******",
				Public: entities.UserPublic{
					LastName:  "Smith",
					FirstName: "Sally",
				},
			}
			rev, err := um.Create(&subUser, smithUser())
			Convey("The revision should be set and the error should be nil", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})
			Convey("The user should have the appropriate roles", func() {
				So(util.HasRole(subUser.Roles, "all_users"), ShouldEqual, true)
			})
		})
		Convey("When the user is updated", func() {
			auth := couchdb.BasicAuth{Username: "******", Password: "******"}
			updateUser := entities.User{}
			rev, err := um.Read("Sally.Smith", &updateUser, smithUser())
			So(err, ShouldBeNil)
			curUser := entities.CurrentUserInfo{
				User: &updateUser,
				Auth: &auth,
			}

			updateUser.Public.MiddleName = "Marie"
			rev, err = um.Update("Sally.Smith", rev, &updateUser, &curUser)
			Convey("Error should be nil and the revision should be set", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})
			rev, err = um.Read("Sally.Smith", &updateUser, smithUser())
			Convey("The user's password should still be valid", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})

		})
		Convey("When the user's password is changed", func() {
			auth := couchdb.BasicAuth{Username: "******", Password: "******"}
			updateUser := entities.User{}
			rev, err := um.Read("Sally.Smith", &updateUser, smithUser())
			So(err, ShouldBeNil)
			curUser := entities.CurrentUserInfo{
				User: &updateUser,
				Auth: &auth,
			}
			newPassword := "******"
			cpr := user_service.ChangePasswordRequest{
				NewPassword: newPassword,
				OldPassword: "******",
			}
			rev, err = um.ChangePassword("Sally.Smith", rev, &cpr, &curUser)
			Convey("Error should be nil and the revision should be set", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})
			rev, err = um.Read("Sally.Smith", &updateUser, &curUser)
			Convey("Old password should NOT work", func() {
				So(err, ShouldNotBeNil)
			})
			curUser.Auth = &couchdb.BasicAuth{Username: "******", Password: "******"}
			rev, err = um.Read("Sally.Smith", &updateUser, &curUser)
			Convey("New password should work", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})

		})
		Convey("When a user is granted a role", func() {
			curUser := smithUser()
			roleRequest := user_service.RoleRequest{
				ResourceType: "main",
				ResourceId:   "",
				AccessType:   "write",
			}
			rev, err := um.GrantRole("Sally.Smith", &roleRequest, curUser)
			Convey("The revision should be set and the error should be nil", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})
			Convey("User should have her new role", func() {
				readUser := new(entities.User)
				_, err := um.Read("Sally.Smith", readUser, smithUser())
				So(err, ShouldBeNil)
				t.Logf("Sally's Record: %v", readUser)
				searchRole := "main_" + ":write"
				searchRoleFound := false
				if readUser.Roles[2] == searchRole {
					searchRoleFound = true
				}
				So(searchRoleFound, ShouldEqual, true)
			})

		})
		Convey("When a user has a role revoked", func() {
			curUser := smithUser()
			roleRequest := user_service.RoleRequest{
				ResourceType: "main",
				ResourceId:   "",
				AccessType:   "write",
			}
			rev, err := um.RevokeRole("Sally.Smith", &roleRequest, curUser)
			Convey("The revision should be set and the error should be nil", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})
			Convey("User should no longer have her role", func() {
				readUser := new(entities.User)
				_, err := um.Read("Sally.Smith", readUser, smithUser())
				So(err, ShouldBeNil)
				searchRole := "main" + ":write"
				searchRoleFound := false
				if len(readUser.Roles) > 1 && readUser.Roles[1] == searchRole {
					searchRoleFound = true
				}
				So(searchRoleFound, ShouldEqual, false)

			})
		})
		Convey("When the user list is requested", func() {
			userList := user_service.UserListQueryResponse{}
			err := um.GetUserList(1, 5, &userList, smithUser())
			Convey("Error should be nil and we should have some results", func() {
				So(err, ShouldBeNil)
				So(len(userList.Rows) >= 2, ShouldBeTrue)
				t.Logf("UserListResponse: %v", userList)
			})
		})
		Convey("When the user is read", func() {
			readUser := entities.User{}
			rev, err := um.Read(user.UserName, &readUser, smithUser())
			Convey("The revision should be set and the error should be nil", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
			})
			Convey("The user data should be set", func() {
				So(readUser.CreatedAt, ShouldNotBeNil)
				So(readUser.UserName, ShouldEqual, "Steven.Smith")
			})
		})
		Convey("When the user by roles list is requested", func() {
			userList := user_service.UserListQueryResponse{}
			err := um.GetUserListForRole(1, 5, []string{"all_users"},
				&userList, smithUser())
			Convey("The error should be nil and we should have some results", func() {
				So(err, ShouldBeNil)
				t.Logf("Response: %v", userList)
			})
		})
		Convey("When a user password reset is requested", func() {
			err := um.RequestPasswordReset("Steven.Smith")
			Convey("The error should indicate no notifcation services are present", func() {
				So(err.Error(), ShouldEqual, "No notifications services listed!")
			})
		})
		Convey("When the user is deleted", func() {
			rev, err := um.Delete("Sally.Smith", smithUser())
			Convey("The revision should be set and the error should be nil", func() {
				So(err, ShouldBeNil)
				So(rev, ShouldNotEqual, "")
				t.Logf("Deleted User rev: %v", rev)
			})
		})
	})
}