mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-21 23:21:26 +01:00
the user can be added again after deletion
This commit is contained in:
parent
ce7d9fcefb
commit
218057d5ba
@ -38,8 +38,14 @@ insert into role (role_code, name) values
|
||||
|
||||
create table user (
|
||||
user_id int NOT NULL AUTO_INCREMENT,
|
||||
username varchar(15),
|
||||
email varchar(128),
|
||||
# The max length of username controlled by API is 20,
|
||||
# and 11 is reserved for marking the deleted users.
|
||||
# The mark of deleted user is "#user_id".
|
||||
# The 11 consist of 10 for the max value of user_id(4294967295)
|
||||
# in MySQL and 1 of '#'.
|
||||
username varchar(32),
|
||||
# 11 bytes is reserved for marking the deleted users.
|
||||
email varchar(255),
|
||||
password varchar(40) NOT NULL,
|
||||
realname varchar (20) NOT NULL,
|
||||
comment varchar (30),
|
||||
|
@ -889,20 +889,6 @@ func TestGetTopRepos(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeleteUser(t *testing.T) {
|
||||
err := DeleteUser(currentUser.UserID)
|
||||
if err != nil {
|
||||
t.Errorf("Error occurred in DeleteUser: %v", err)
|
||||
}
|
||||
user, err := GetUser(*currentUser)
|
||||
if err != nil {
|
||||
t.Errorf("Error occurred in GetUser: %v", err)
|
||||
}
|
||||
if user != nil {
|
||||
t.Errorf("user is not nil after deletion, user: %+v", user)
|
||||
}
|
||||
}
|
||||
|
||||
var targetID, policyID, policyID2, policyID3, jobID, jobID2, jobID3 int64
|
||||
|
||||
func TestAddRepTarget(t *testing.T) {
|
||||
|
@ -214,7 +214,10 @@ func CheckUserPassword(query models.User) (*models.User, error) {
|
||||
// DeleteUser ...
|
||||
func DeleteUser(userID int) error {
|
||||
o := GetOrmer()
|
||||
_, err := o.Raw(`update user set deleted = 1 where user_id = ?`, userID).Exec()
|
||||
_, err := o.Raw(`update user
|
||||
set deleted = 1, username = concat(username, "#", user_id),
|
||||
email = concat(email, "#", user_id)
|
||||
where user_id = ?`, userID).Exec()
|
||||
return err
|
||||
}
|
||||
|
||||
|
69
dao/user_test.go
Normal file
69
dao/user_test.go
Normal file
@ -0,0 +1,69 @@
|
||||
/*
|
||||
Copyright (c) 2016 VMware, Inc. All Rights Reserved.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/vmware/harbor/models"
|
||||
)
|
||||
|
||||
func TestDeleteUser(t *testing.T) {
|
||||
username := "user_for_test"
|
||||
email := "user_for_test@vmware.com"
|
||||
password := "P@ssword"
|
||||
realname := "user_for_test"
|
||||
|
||||
u := models.User{
|
||||
Username: username,
|
||||
Email: email,
|
||||
Password: password,
|
||||
Realname: realname,
|
||||
}
|
||||
id, err := Register(u)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to register user: %v", err)
|
||||
}
|
||||
|
||||
err = DeleteUser(int(id))
|
||||
if err != nil {
|
||||
t.Fatalf("Error occurred in DeleteUser: %v", err)
|
||||
}
|
||||
|
||||
user := &models.User{}
|
||||
sql := "select * from user where user_id = ?"
|
||||
if err = GetOrmer().Raw(sql, id).
|
||||
QueryRow(user); err != nil {
|
||||
t.Fatalf("failed to query user: %v", err)
|
||||
}
|
||||
|
||||
if user.Deleted != 1 {
|
||||
t.Error("user is not deleted")
|
||||
}
|
||||
|
||||
expected := fmt.Sprintf("%s#%d", u.Username, id)
|
||||
if user.Username != expected {
|
||||
t.Errorf("unexpected username: %s != %s", user.Username,
|
||||
expected)
|
||||
}
|
||||
|
||||
expected = fmt.Sprintf("%s#%d", u.Email, id)
|
||||
if user.Email != expected {
|
||||
t.Errorf("unexpected email: %s != %s", user.Email,
|
||||
expected)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user