loading up the forgejo repo on tangled to test page performance
1// Copyright 2024 The Gitea Authors. All rights reserved.
2// SPDX-License-Identifier: MIT
3
4package integration
5
6import (
7 "fmt"
8 "net/http"
9 "testing"
10
11 auth_model "forgejo.org/models/auth"
12 api "forgejo.org/modules/structs"
13 "forgejo.org/tests"
14)
15
16func TestAPIUserVariables(t *testing.T) {
17 defer tests.PrepareTestEnv(t)()
18
19 session := loginUser(t, "user1")
20 token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
21
22 t.Run("CreateUserVariable", func(t *testing.T) {
23 cases := []struct {
24 Name string
25 ExpectedStatus int
26 }{
27 {
28 Name: "-",
29 ExpectedStatus: http.StatusBadRequest,
30 },
31 {
32 Name: "_",
33 ExpectedStatus: http.StatusNoContent,
34 },
35 {
36 Name: "TEST_VAR",
37 ExpectedStatus: http.StatusNoContent,
38 },
39 {
40 Name: "test_var",
41 ExpectedStatus: http.StatusConflict,
42 },
43 {
44 Name: "ci",
45 ExpectedStatus: http.StatusBadRequest,
46 },
47 {
48 Name: "123var",
49 ExpectedStatus: http.StatusBadRequest,
50 },
51 {
52 Name: "var@test",
53 ExpectedStatus: http.StatusBadRequest,
54 },
55 {
56 Name: "github_var",
57 ExpectedStatus: http.StatusBadRequest,
58 },
59 {
60 Name: "gitea_var",
61 ExpectedStatus: http.StatusBadRequest,
62 },
63 }
64
65 for _, c := range cases {
66 req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/user/actions/variables/%s", c.Name), api.CreateVariableOption{
67 Value: "value",
68 }).AddTokenAuth(token)
69 MakeRequest(t, req, c.ExpectedStatus)
70 }
71 })
72
73 t.Run("UpdateUserVariable", func(t *testing.T) {
74 variableName := "test_update_var"
75 url := fmt.Sprintf("/api/v1/user/actions/variables/%s", variableName)
76 req := NewRequestWithJSON(t, "POST", url, api.CreateVariableOption{
77 Value: "initial_val",
78 }).AddTokenAuth(token)
79 MakeRequest(t, req, http.StatusNoContent)
80
81 cases := []struct {
82 Name string
83 UpdateName string
84 ExpectedStatus int
85 }{
86 {
87 Name: "not_found_var",
88 ExpectedStatus: http.StatusNotFound,
89 },
90 {
91 Name: variableName,
92 UpdateName: "1invalid",
93 ExpectedStatus: http.StatusBadRequest,
94 },
95 {
96 Name: variableName,
97 UpdateName: "invalid@name",
98 ExpectedStatus: http.StatusBadRequest,
99 },
100 {
101 Name: variableName,
102 UpdateName: "ci",
103 ExpectedStatus: http.StatusBadRequest,
104 },
105 {
106 Name: variableName,
107 UpdateName: "updated_var_name",
108 ExpectedStatus: http.StatusNoContent,
109 },
110 {
111 Name: variableName,
112 ExpectedStatus: http.StatusNotFound,
113 },
114 {
115 Name: "updated_var_name",
116 ExpectedStatus: http.StatusNoContent,
117 },
118 }
119
120 for _, c := range cases {
121 req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/user/actions/variables/%s", c.Name), api.UpdateVariableOption{
122 Name: c.UpdateName,
123 Value: "updated_val",
124 }).AddTokenAuth(token)
125 MakeRequest(t, req, c.ExpectedStatus)
126 }
127 })
128
129 t.Run("DeleteRepoVariable", func(t *testing.T) {
130 variableName := "test_delete_var"
131 url := fmt.Sprintf("/api/v1/user/actions/variables/%s", variableName)
132
133 req := NewRequestWithJSON(t, "POST", url, api.CreateVariableOption{
134 Value: "initial_val",
135 }).AddTokenAuth(token)
136 MakeRequest(t, req, http.StatusNoContent)
137
138 req = NewRequest(t, "DELETE", url).AddTokenAuth(token)
139 MakeRequest(t, req, http.StatusNoContent)
140
141 req = NewRequest(t, "DELETE", url).AddTokenAuth(token)
142 MakeRequest(t, req, http.StatusNotFound)
143 })
144}