-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
application_template_test.go
72 lines (60 loc) · 2.65 KB
/
application_template_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package charon_test
import (
"bytes"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/go/x"
"gitlab.com/tozd/waf"
"gitlab.com/charon/charon"
)
func createApplicationTemplate(t *testing.T, ts *httptest.Server, service *charon.Service) *charon.ApplicationTemplate {
t.Helper()
applicationTemplateCreate, errE := service.ReverseAPI("ApplicationTemplateCreate", nil, nil)
require.NoError(t, errE, "% -+#.1v", errE)
homepage := "https://example.com"
applicationTemplate := charon.ApplicationTemplate{
ApplicationTemplatePublic: charon.ApplicationTemplatePublic{
Name: "Test application",
Description: "",
HomepageTemplate: &homepage,
IDScopes: []string{"openid", "offline_access"},
Variables: []charon.Variable{},
ClientsPublic: []charon.ApplicationTemplateClientPublic{},
ClientsBackend: []charon.ApplicationTemplateClientBackend{
{
Description: "",
AdditionalScopes: []string{},
TokenEndpointAuthMethod: "client_secret_post",
RedirectURITemplates: []string{"https://example.com/redirect"},
},
},
ClientsService: []charon.ApplicationTemplateClientService{},
},
}
data, errE := x.MarshalWithoutEscapeHTML(applicationTemplate)
require.NoError(t, errE, "% -+#.1v", errE)
resp, err := ts.Client().Post(ts.URL+applicationTemplateCreate, "application/json", bytes.NewReader(data)) //nolint:noctx,bodyclose
require.NoError(t, err)
t.Cleanup(func(r *http.Response) func() { return func() { r.Body.Close() } }(resp))
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 2, resp.ProtoMajor)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
var applicationTemplateRef charon.ApplicationTemplateRef
errE = x.DecodeJSONWithoutUnknownFields(resp.Body, &applicationTemplateRef)
require.NoError(t, errE, "% -+#.1v", errE)
applicationTemplateGet, errE := service.ReverseAPI("ApplicationTemplateGet", waf.Params{"id": applicationTemplateRef.ID.String()}, nil)
require.NoError(t, errE, "% -+#.1v", errE)
resp, err = ts.Client().Get(ts.URL + applicationTemplateGet) //nolint:noctx,bodyclose
require.NoError(t, err)
t.Cleanup(func(r *http.Response) func() { return func() { r.Body.Close() } }(resp))
assert.Equal(t, http.StatusOK, resp.StatusCode)
assert.Equal(t, 2, resp.ProtoMajor)
assert.Equal(t, "application/json", resp.Header.Get("Content-Type"))
var newApplicationTemplate charon.ApplicationTemplate
errE = x.DecodeJSONWithoutUnknownFields(resp.Body, &newApplicationTemplate)
require.NoError(t, errE, "% -+#.1v", errE)
return &newApplicationTemplate
}