diff --git a/.github/workflows/workflow.yaml b/.github/workflows/workflow.yaml new file mode 100644 index 00000000..eb0fd6a8 --- /dev/null +++ b/.github/workflows/workflow.yaml @@ -0,0 +1,30 @@ +name: User interface test + +jobs: + build: + runs-on: ubuntu-latest + + steps: + - uses: actions/checkout@v3 + + - name: Set up golang + uses: actions/setup-go@v4 + with: + go-version: '1.20.x' + + - name: Set up environment + uses: isbang/compose-action@v1.5.1 + with: + compose-file: "./docker-compose.ci.yml" + + - name: Install dependencies + run: go mod tidy + + - name: Build target + run: make user + + - name: Run local server + run: cd ./output/user && nohup sh bootstrap.sh > nohup.out 2> nohup.err < /dev/null & + + - name: Test interfaces + run: cd ../../test && go test -v \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 00000000..bdd586bc --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +DIR = $(shell pwd) +CMD = $(DIR)/CMD +CONFIG_PATH = $(DIR)/config +IDL_PATH = $(DIR)/idl +OUTPUT_PATH = $(DIR)/output + +SERVICES := api user follow interaction video +service = $(word 1, $@) + +# mock gen +MOCKS := user_mock +mock = $(word 1, $@) + +.PHONY: env-up +env-up: + docker-compose up -d + +.PHONY: env-down +env-down: + docker-compose down + +.PHONY: $(SERVICES) +$(SERVICES): + mkdir -p output + cd $(CMD)/$(service) && sh build.sh + cd $(CMD)/$(service)/output && cp -r . $(OUTPUT_PATH)/$(service) + sh $(OUTPUT_PATH)/$(service)/bootstrap.sh + + +.PHONY: $(MOCKS) +$(MOCKS): + @mkdir -p mocks + mockgen -source=./idl/$(mock).go -destination=./mocks/$(mock).go -package=mocks \ No newline at end of file diff --git a/cmd/user/dal/db/user.go b/cmd/user/dal/db/user.go index 2ad25286..1b529700 100644 --- a/cmd/user/dal/db/user.go +++ b/cmd/user/dal/db/user.go @@ -5,6 +5,7 @@ import ( "errors" "time" + "github.com/ozline/tiktok/pkg/errno" "gorm.io/gorm" ) @@ -29,6 +30,18 @@ TODO: follow_count, follower_count, is_follow, work_count, favorite_count, total */ func CreateUser(ctx context.Context, user *User) (*User, error) { + userResp := new(User) + + err := DB.WithContext(ctx).Where("username = ?", user.Username).First(&userResp).Error + + if err == nil { + return nil, errno.UserExistedError + } + + if !errors.Is(err, gorm.ErrRecordNotFound) { + return nil, err + } + if err := DB.WithContext(ctx).Create(user).Error; err != nil { // add some logs return nil, err @@ -46,7 +59,7 @@ func GetUserByUsername(ctx context.Context, username string) (*User, error) { // add some logs if errors.Is(err, gorm.ErrRecordNotFound) { - return nil, errors.New("User not found") + return nil, err } return nil, err } diff --git a/cmd/user/script/bootstrap.sh b/cmd/user/script/bootstrap.sh index b02fa71f..401cc7e2 100644 --- a/cmd/user/script/bootstrap.sh +++ b/cmd/user/script/bootstrap.sh @@ -1,6 +1,6 @@ #! /usr/bin/env bash CURDIR=$(cd $(dirname $0); pwd) -CONFIG_PATH=$(dirname $(dirname $(dirname $CURDIR)))/config +CONFIG_PATH=$(dirname $(dirname $CURDIR))/config if [ "X$1" != "X" ]; then RUNTIME_ROOT=$1 diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml new file mode 100644 index 00000000..033518f1 --- /dev/null +++ b/docker-compose.ci.yml @@ -0,0 +1,51 @@ +version: '3.7' + +networks: + tiktok: + driver: bridge + +services: + + mysql: + container_name: mysql + image: mysql:latest + restart: always + environment: + - MYSQL_ROOT_PASSWORD=root + - MYSQL_DATABASE=tiktok + - MYSQL_USER=tiktok + - MYSQL_PASSWORD=tiktok + - TZ=Asia/Shanghai + volumes: + - ./config/sql:/docker-entrypoint-initdb.d/ + ports: + - "3306:3306" + networks: + - tiktok + + redis: + container_name: redis + image: "redis:latest" + restart: always + ports: + - 6379:6379 + volumes: + - /usr/local/redis/conf/redis.conf:/var/lib/redis/conf/redis.conf + - /usr/local/redis/data:/data + environment: + - REDIS_PASSWORD=tiktok + - ALLOW_EMPTY_PASSWORD=no + networks: + - tiktok + + rabbitmq: + container_name: rabiitmq + image: "rabbitmq:latest" + ports: + - 5672:5672 + - 15672:15672 + environment: + - RABBITMQ_DEFAULT_USER=tiktok + - RABBITMQ_DEFAULT_PASS=tiktok + networks: + - tiktok \ No newline at end of file diff --git a/pkg/errno/code.go b/pkg/errno/code.go index d4a02a11..67f9b2cf 100644 --- a/pkg/errno/code.go +++ b/pkg/errno/code.go @@ -16,5 +16,5 @@ const ( AuthorizationFailedErrCode = 10003 // 鉴权失败 UnexpectedTypeErrorCode = 10004 // 未知类型 NotImplementErrorCode = 10005 // 未实装 - SensitiveWordsErrorCode = 10006 //敏感词错误 + SensitiveWordsErrorCode = 10006 // 敏感词错误 ) diff --git a/cmd/user/test/info_test.go b/test/user/info_test.go similarity index 70% rename from cmd/user/test/info_test.go rename to test/user/info_test.go index d3c2b49a..fb221e46 100644 --- a/cmd/user/test/info_test.go +++ b/test/user/info_test.go @@ -8,13 +8,20 @@ import ( "github.com/cloudwego/kitex/client/callopt" "github.com/ozline/tiktok/kitex_gen/user" "github.com/ozline/tiktok/pkg/errno" + "github.com/ozline/tiktok/pkg/utils" ) -func TestGetUserInfo(t *testing.T) { - TestLogin(t) - t.Logf("Token: %v", token) +func testGetUserInfo(t *testing.T) { + + token, err := utils.CreateToken(10001) + + if err != nil { + t.Error(err) + t.Fail() + } + req := &user.InfoRequest{ - UserId: 10001, // 按需修改账号 + UserId: id, // 按需修改账号 Token: token, } @@ -29,6 +36,4 @@ func TestGetUserInfo(t *testing.T) { t.Error(errno.NewErrNo(resp.Base.Code, resp.Base.Msg)) t.Fail() } - - t.Logf("Resp:\n%v\n\n", resp) } diff --git a/cmd/user/test/login_test.go b/test/user/login_test.go similarity index 89% rename from cmd/user/test/login_test.go rename to test/user/login_test.go index 52b6955c..fc16c370 100644 --- a/cmd/user/test/login_test.go +++ b/test/user/login_test.go @@ -10,7 +10,7 @@ import ( "github.com/ozline/tiktok/pkg/errno" ) -func TestLogin(t *testing.T) { +func testLogin(t *testing.T) { req := &user.LoginRequest{ Username: "ozline", Password: "123456", @@ -28,7 +28,6 @@ func TestLogin(t *testing.T) { t.Fail() } - t.Logf("Resp:\n%v\n\n", resp) - token = resp.Token + id = resp.User.Id } diff --git a/test/user/main_test.go b/test/user/main_test.go new file mode 100644 index 00000000..39d9a032 --- /dev/null +++ b/test/user/main_test.go @@ -0,0 +1,45 @@ +package main + +import ( + "testing" + + "github.com/ozline/tiktok/pkg/constants" + + "github.com/cloudwego/kitex/client" + "github.com/ozline/tiktok/kitex_gen/user/userservice" +) + +var ( + conn userservice.Client + username string + password string + token string + id int64 +) + +func TestMain(m *testing.M) { + // 连接服务器 + c, err := userservice.NewClient("user", + client.WithMuxConnection(constants.MuxConnection), + client.WithHostPorts("0.0.0.0:10002")) + + if err != nil { + panic(err) + } + + username = "ozline" + password = "123456" + + conn = c + m.Run() +} + +func TestMainOrder(t *testing.T) { + t.Run("register", testRegister) + + t.Run("login", testLogin) + + t.Run("info", testGetUserInfo) + + t.Run("RPC Test", testRPC) +} diff --git a/cmd/user/test/register_test.go b/test/user/register_test.go similarity index 57% rename from cmd/user/test/register_test.go rename to test/user/register_test.go index b57ae362..6c4d0222 100644 --- a/cmd/user/test/register_test.go +++ b/test/user/register_test.go @@ -10,24 +10,21 @@ import ( "github.com/ozline/tiktok/pkg/errno" ) -func TestRegister(t *testing.T) { - req := &user.RegisterRequest{ - Username: "ozline", - Password: "123456", - } +func testRegister(t *testing.T) { - resp, err := conn.Register(context.Background(), req, callopt.WithRPCTimeout(3*time.Second)) + resp, err := conn.Register(context.Background(), &user.RegisterRequest{ + Username: username, + Password: password, + }, callopt.WithRPCTimeout(3*time.Second)) if err != nil { t.Error(err) - t.FailNow() - + t.Fail() } if resp.Base.Code != errno.SuccessCode { t.Error(errno.NewErrNo(resp.Base.Code, resp.Base.Msg)) - t.Fail() } - t.Logf("Resp:\n%v\n\n", resp) + // t.Logf("Resp:\n%v\n\n", resp) } diff --git a/cmd/user/test/main_test.go b/test/user/rpc_test.go similarity index 54% rename from cmd/user/test/main_test.go rename to test/user/rpc_test.go index 58093840..51fdc20e 100644 --- a/cmd/user/test/main_test.go +++ b/test/user/rpc_test.go @@ -1,26 +1,20 @@ package main import ( - "github.com/ozline/tiktok/pkg/constants" "testing" "github.com/cloudwego/kitex/client" "github.com/ozline/tiktok/kitex_gen/user/userservice" + "github.com/ozline/tiktok/pkg/constants" ) -var conn userservice.Client -var token string - -func TestMain(m *testing.M) { - // 连接服务器 - c, err := userservice.NewClient("user", +func testRPC(t *testing.T) { + _, err := userservice.NewClient("user", client.WithMuxConnection(constants.MuxConnection), - client.WithHostPorts("0.0.0.0:8888")) + client.WithHostPorts("0.0.0.0:10002")) if err != nil { - panic(err) + t.Error(err) + t.Fail() } - - conn = c - m.Run() }