//  [go]
func TestUsersPost(t *testing.T) {

	fmt.Println("Testing User Add")

	assert := assert.New(t)
	apiTest := newHarborAPI()
	config.Upload(map[string]interface{}{
		common.AUTHMode: "db_auth",
	})
	// case 1: register a new user without admin auth, expect 400, because self registration is on
	fmt.Println("Register user without admin auth")
	code, err := apiTest.UsersPost(testUser0002)
	if err != nil {
		t.Error("Error occurred while add a test User", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 2: register a new user with admin auth, but username is empty, expect 400
	fmt.Println("Register user with admin auth, but username is empty")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 3: register a new user with admin auth, but bad username format, expect 400
	testUser0002.Username = "test@$"
	fmt.Println("Register user with admin auth, but bad username format")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 4: register a new user with admin auth, but bad userpassword format, expect 400
	testUser0002.Username = "testUser0002"
	fmt.Println("Register user with admin auth, but empty password.")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 5: register a new user with admin auth, but email is empty, expect 400
	testUser0002.Password = "testUser0002"
	fmt.Println("Register user with admin auth, but email is empty")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 6: register a new user with admin auth, but bad email format, expect 400
	testUser0002.Email = "test..."
	fmt.Println("Register user with admin auth, but bad email format")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 7: register a new user with admin auth, but userrealname is empty, expect 400
	/*
		testUser0002.Email = "testUser0002@mydomain.com"
		fmt.Println("Register user with admin auth, but user realname is empty")
		code, err = apiTest.UsersPost(testUser0002, *admin)
		if err != nil {
			t.Error("Error occurred while add a user", err.Error())
			t.Log(err)
		} else {
			assert.Equal(400, code, "Add user status should be 400")
		}
	*/
	// case 8: register a new user with admin auth, but bad userrealname format, expect 400
	testUser0002.Email = "testUser0002@mydomain.com"
	testUser0002.Realname = "test$com"
	fmt.Println("Register user with admin auth, but bad user realname format")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)

	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 9: register a new user with admin auth, but bad user comment, expect 400
	testUser0002.Realname = "testUser0002"
	testUser0002.Comment = "vmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmmm"
	fmt.Println("Register user with admin auth, but user comment length is illegal")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(400, code, "Add user status should be 400")
	}

	// case 10: register a new user with admin auth, expect 201
	fmt.Println("Register user with admin auth, right parameters")
	testUser0002.Comment = "test user"
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(201, code, "Add user status should be 201")
	}

	// case 11: register duplicate user with admin auth, expect 409
	fmt.Println("Register duplicate user with admin auth")
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(409, code, "Add user status should be 409")
	}

	// case 12: register a new user with admin auth, but duplicate email, expect 409
	fmt.Println("Register user with admin auth, but duplicate email")
	testUser0002.Username = "testUsertest"
	testUser0002.Email = "testUser0002@mydomain.com"
	code, err = apiTest.UsersPost(testUser0002, *admin)
	if err != nil {
		t.Error("Error occurred while add a user", err.Error())
		t.Log(err)
	} else {
		assert.Equal(409, code, "Add user status should be 409")
	}
}