api_case_test.go 4.2 KB
Newer Older
aaronchen2k2k's avatar
aaronchen2k2k 已提交
1 2 3 4 5 6 7 8 9 10
package main

import (
	"fmt"
	commConsts "github.com/easysoft/zentaoatf/internal/pkg/consts"
	commDomain "github.com/easysoft/zentaoatf/internal/pkg/domain"
	zentaoHelper "github.com/easysoft/zentaoatf/internal/pkg/helper/zentao"
	stringUtils "github.com/easysoft/zentaoatf/pkg/lib/string"
	constTestHelper "github.com/easysoft/zentaoatf/test/helper/conf"
	httpHelper "github.com/easysoft/zentaoatf/test/helper/http"
11
	"github.com/easysoft/zentaoatf/test/restapi/config"
aaronchen2k2k's avatar
aaronchen2k2k 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
	"github.com/ozontech/allure-go/pkg/framework/provider"
	"github.com/ozontech/allure-go/pkg/framework/suite"
	"github.com/tidwall/gjson"
	"testing"
)

func TestCaseApi(t *testing.T) {
	suite.RunSuite(t, new(CaseApiSuite))
}

type CaseApiSuite struct {
	suite.Suite
}

func (s *CaseApiSuite) BeforeEach(t provider.T) {
	t.AddSubSuite("CaseApi")
}

func (s *CaseApiSuite) TestCaseListApi(t provider.T) {
31
	t.ID("7612")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
32 33 34 35 36
	token := httpHelper.Login()

	params := map[string]interface{}{
		"limit": 10,
	}
37
	url := zentaoHelper.GenApiUrl(fmt.Sprintf("/products/%d/testcases", config.ProductId), params, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
38 39 40 41 42 43 44 45 46

	bodyBytes, _ := httpHelper.Get(url, token)

	firstCaseId := gjson.Get(string(bodyBytes), "testcases.0.id").Int()

	t.Require().Greater(firstCaseId, int64(0), "list testcases failed")
}

func (s *CaseApiSuite) TestCaseListByModuleApi(t provider.T) {
47
	t.ID("7635")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
48 49
	token := httpHelper.Login()

aaronchen2k2k's avatar
aaronchen2k2k 已提交
50 51 52
	moduleId := getModuleMinId()

	url := zentaoHelper.GenApiUrl(fmt.Sprintf("/products/%d/testcases?module=%d", config.ProductId, moduleId),
aaronchen2k2k's avatar
aaronchen2k2k 已提交
53
		nil, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
54 55 56 57 58 59 60 61 62

	bodyBytes, _ := httpHelper.Get(url, token)

	firstCaseId := gjson.Get(string(bodyBytes), "testcases.0.id").Int()

	t.Require().Greater(firstCaseId, int64(0), "list testcases failed")
}

func (s *CaseApiSuite) TestCaseListBySuiteApi(t provider.T) {
63
	t.ID("7614")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
64 65
	token := httpHelper.Login()

66
	url := zentaoHelper.GenApiUrl(fmt.Sprintf("/testsuites/%d", config.SuiteId), nil, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
67 68 69 70 71 72 73 74 75

	bodyBytes, _ := httpHelper.Get(url, token)

	firstCaseId := gjson.Get(string(bodyBytes), "testcases.0.id").Int()

	t.Require().Greater(firstCaseId, int64(0), "list testcases failed")
}

func (s *CaseApiSuite) TestCaseListByTaskApi(t provider.T) {
76
	t.ID("7615")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
77 78
	token := httpHelper.Login()

79
	url := zentaoHelper.GenApiUrl(fmt.Sprintf("/testtasks/%d", config.TaskId), nil, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
80 81 82 83 84 85 86 87 88

	bodyBytes, _ := httpHelper.Get(url, token)

	firstCaseId := gjson.Get(string(bodyBytes), "testcases.0.id").Int()

	t.Require().Greater(firstCaseId, int64(0), "list testcases failed")
}

func (s *CaseApiSuite) TestCaseDetailApi(t provider.T) {
89
	t.ID("7613")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
90 91
	token := httpHelper.Login()

92
	url := zentaoHelper.GenApiUrl(fmt.Sprintf("testcases/%d", config.CaseId), nil, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
93 94 95 96 97 98 99 100 101

	bodyBytes, _ := httpHelper.Get(url, token)

	title := gjson.Get(string(bodyBytes), "title").String()

	t.Require().Greater(len(title), 0, "get testcases failed")
}

func (s *CaseApiSuite) TestCaseCheckinApi(t provider.T) {
102
	t.ID("7616")
aaronchen2k2k's avatar
aaronchen2k2k 已提交
103
	token := httpHelper.Login()
104

105
	url := zentaoHelper.GenApiUrl(fmt.Sprintf("testcases/%d", config.CaseId), nil, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

	steps := []commDomain.ZentaoCaseStep{
		{Type: commConsts.Item, Desc: "Step 1", Expect: "Expect 1"},
		{Type: commConsts.Item, Desc: "Step 2", Expect: "Expect 1"},
		{Type: commConsts.Item, Desc: "Step 3", Expect: "Expect 1"},
	}

	title := "用例新名字" + stringUtils.NewUuid()
	requestObj := map[string]interface{}{
		"type":  "feature",
		"title": title,
		"steps": steps,

		"path":   "path_of_case",
		"script": "script_of_case",
		"lang":   "php",
	}

	bodyBytes, _ := httpHelper.Put(url, token, requestObj)

	actualTitle := gjson.Get(string(bodyBytes), "title").String()
	t.Require().Equal(actualTitle, title, "checkin testcases failed")

129
	newCase := getCase(config.CaseId)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
130 131 132 133 134 135 136
	titleFromRemote := newCase["title"]
	t.Require().Equal(titleFromRemote, title, "get testcases failed")
}

func getCase(id int) (cs map[string]interface{}) {
	token := httpHelper.Login()

aaronchen2k2k's avatar
aaronchen2k2k 已提交
137
	url := zentaoHelper.GenApiUrl(fmt.Sprintf("testcases/%d", id), nil, constTestHelper.ZentaoSiteUrl)
aaronchen2k2k's avatar
aaronchen2k2k 已提交
138 139 140 141 142 143 144 145 146 147

	bodyBytes, _ := httpHelper.Get(url, token)

	cs = map[string]interface{}{}

	cs["id"] = gjson.Get(string(bodyBytes), "id").Int()
	cs["title"] = gjson.Get(string(bodyBytes), "title").String()

	return
}