Skip to content

Commit

Permalink
new-register (Ptt-official-app#14)
Browse files Browse the repository at this point in the history
pass tests
  • Loading branch information
chhsiao1981 authored Nov 26, 2020
1 parent a7cc099 commit 9da66f0
Show file tree
Hide file tree
Showing 90 changed files with 3,261 additions and 585 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/ptt/.fresh
16 changes: 16 additions & 0 deletions 00-config.template.ini
Original file line number Diff line number Diff line change
Expand Up @@ -476,3 +476,19 @@ FN_SAFEDEL_PREFIX_LEN = 8 # // must match FN_SAFEDEL
STR_SAFEDEL_TITLE = (本文已被刪除)

SAFE_ARTICLE_DELETE_NUSER = 2

# //////////
# // proto.h
# //////////
USE_COMMENTD = false
USE_EMAILDB = false
USE_REGCHECKD = false
USE_VERIFYDB = false

# //////////
# //////////
# // types
# //////////
# //////////
[types]
TIME_LOCATION = Asia/Taipei
1 change: 1 addition & 0 deletions api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import "errors"

var (
ErrInvalidParams = errors.New("invalid params")
ErrLoginFailed = errors.New("login failed")
)
2 changes: 1 addition & 1 deletion api/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func Login(params interface{}) (interface{}, error) {

user, err := bbs.Login(loginParams.UserID, loginParams.Passwd, loginParams.IP)
if err != nil {
return nil, err
return nil, ErrLoginFailed
}

token, err := createToken(user)
Expand Down
14 changes: 7 additions & 7 deletions api/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ func TestLogin(t *testing.T) {
params interface{}
}
tests := []struct {
name string
args args
want interface{}
wantErr bool
name string
args args
expected interface{}
wantErr bool
}{
// TODO: Add test cases.
{
args: args{params: &LoginParams{
UserID: "SYSOP",
Passwd: "123123",
}},
want: &JwtClaim{UserID: "SYSOP"},
expected: &JwtClaim{UserID: "SYSOP"},
},
}

Expand All @@ -47,9 +47,9 @@ func TestLogin(t *testing.T) {
claims := &JwtClaim{}
token, _ := jwt.ParseSigned(gotResult.Jwt)
_ = token.UnsafeClaimsWithoutVerification(claims)
wantJwt, _ := tt.want.(*JwtClaim)
wantJwt, _ := tt.expected.(*JwtClaim)
if !reflect.DeepEqual(claims.UserID, wantJwt.UserID) {
t.Errorf("Login() = %v claims: %v want: %v", got, claims, tt.want)
t.Errorf("Login() = %v claims: %v expected: %v", got, claims, tt.expected)
return
}
})
Expand Down
54 changes: 54 additions & 0 deletions api/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package api

import "github.com/PichuChen/go-bbs"

type RegisterParams struct {
UserID string
Passwd string
IP string
Email string

Nickname string
Realname string
Career string
Address string
Over18 bool
}

type RegisterResult struct {
Jwt string
}

func Register(params interface{}) (interface{}, error) {
registerParams, ok := params.(*RegisterParams)
if !ok {
return nil, ErrInvalidParams
}

user, err := bbs.Register(
registerParams.UserID,
registerParams.Passwd,
registerParams.IP,
registerParams.Email,

registerParams.Nickname,
registerParams.Realname,
registerParams.Career,
registerParams.Address,
registerParams.Over18,
)
if err != nil {
return nil, err
}

token, err := createToken(user)
if err != nil {
return nil, err
}

result := &RegisterResult{
Jwt: token,
}

return result, nil
}
2 changes: 1 addition & 1 deletion api/testinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func setupTest() {

// shm
cache.IsTest = true
_ = cache.NewSHM(types.Key_t(ptttype.SHM_KEY), ptttype.USE_HUGETLB, true)
_ = cache.NewSHM(types.Key_t(cache.TestShmKey), ptttype.USE_HUGETLB, true)
cache.LoadUHash()
cache.AttachSHM()
}
Expand Down
6 changes: 3 additions & 3 deletions cache/cache_money.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func SetUMoney(uid int32, money int32) (int32, error) {
return money, err
}

return moneyOf(uid), nil
return MoneyOf(uid), nil

}

Expand All @@ -36,15 +36,15 @@ func DeUMoney(uid int32, money int32) (int32, error) {
return -1, ErrInvalidUID
}

currentMoney := moneyOf(uid)
currentMoney := MoneyOf(uid)
if money < 0 && currentMoney < -money {
return SetUMoney(uid, 0)
}

return SetUMoney(uid, currentMoney+money)
}

func moneyOf(uid int32) (money int32) {
func MoneyOf(uid int32) (money int32) {
Shm.ReadAt(
unsafe.Offsetof(Shm.Money)+types.INT32_SZ*uintptr(uid-1),
types.INT32_SZ,
Expand Down
72 changes: 36 additions & 36 deletions cache/cache_money_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ func TestSetUMoney(t *testing.T) {
copy(userID1[:], []byte("SYSOP"))
SetUserID(1, userID1)

money := moneyOf(1)
money := MoneyOf(1)
log.Infof("TestSetUMoney: moneyOf(1): money: %v", money)

type args struct {
uid int32
money int32
}
tests := []struct {
name string
args args
want int32
wantErr bool
name string
args args
expected int32
wantErr bool
}{
// TODO: Add test cases.
{
args: args{1, 100},
want: 100,
args: args{1, 100},
expected: 100,
},
{
args: args{1, 10000},
want: 10000,
args: args{1, 10000},
expected: 10000,
},
{
args: args{1, 0},
want: 0,
args: args{1, 0},
expected: 0,
},
{
args: args{1, money},
want: money,
args: args{1, money},
expected: money,
},
}
for _, tt := range tests {
Expand All @@ -62,8 +62,8 @@ func TestSetUMoney(t *testing.T) {
t.Errorf("SetUMoney() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("SetUMoney() = %v, want %v", got, tt.want)
if got != tt.expected {
t.Errorf("SetUMoney() = %v, expected %v", got, tt.expected)
}
})
}
Expand All @@ -82,7 +82,7 @@ func TestDeUMoney(t *testing.T) {

_ = LoadUHash()

money := moneyOf(1)
money := MoneyOf(1)
log.Infof("TestDeUMoney: moneyOf(1): money: %v", money)

defer SetUMoney(1, 0)
Expand All @@ -92,39 +92,39 @@ func TestDeUMoney(t *testing.T) {
money int32
}
tests := []struct {
name string
args args
want int32
wantErr bool
name string
args args
expected int32
wantErr bool
}{
// TODO: Add test cases.
{
args: args{1, 100},
want: 100,
args: args{1, 100},
expected: 100,
},
{
args: args{1, -50},
want: 50,
args: args{1, -50},
expected: 50,
},
{
args: args{1, -200},
want: 0,
args: args{1, -200},
expected: 0,
},
{
args: args{1, 100},
want: 100,
args: args{1, 100},
expected: 100,
},
{
args: args{1, 300},
want: 400,
args: args{1, 300},
expected: 400,
},
{
args: args{1, -150},
want: 250,
args: args{1, -150},
expected: 250,
},
{
args: args{1, -250},
want: 0,
args: args{1, -250},
expected: 0,
},
}
for _, tt := range tests {
Expand All @@ -134,8 +134,8 @@ func TestDeUMoney(t *testing.T) {
t.Errorf("DeUMoney() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("DeUMoney() = %v, want %v", got, tt.want)
if got != tt.expected {
t.Errorf("DeUMoney() = %v, expected %v", got, tt.expected)
}
})
}
Expand Down
6 changes: 3 additions & 3 deletions cache/cache_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func doSearchUser(userID string, isReturn bool) (uid int32, rightID string, err
rightIDBytes = &[ptttype.IDLEN + 1]byte{}
}

uid, err = doSearchUserRaw(userIDBytes, rightIDBytes)
uid, err = DoSearchUserRaw(userIDBytes, rightIDBytes)
if err != nil {
return 0, "", err
}
Expand All @@ -188,10 +188,10 @@ func SearchUserRaw(userID *[ptttype.IDLEN + 1]byte, rightID *[ptttype.IDLEN + 1]
if userID[0] == 0 {
return 0, nil
}
return doSearchUserRaw(userID, rightID)
return DoSearchUserRaw(userID, rightID)
}

func doSearchUserRaw(userID *[ptttype.IDLEN + 1]byte, rightID *[ptttype.IDLEN + 1]byte) (int32, error) {
func DoSearchUserRaw(userID *[ptttype.IDLEN + 1]byte, rightID *[ptttype.IDLEN + 1]byte) (int32, error) {
// XXX we should have 0 as non-exists.
// currently the reason why it's ok is because the probability of collision on 0 is low.

Expand Down
Loading

0 comments on commit 9da66f0

Please sign in to comment.