-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_integration_test.go
120 lines (106 loc) · 2.58 KB
/
api_integration_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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// integration tests
package graph
import (
"graphpass/graph"
"graphpass/graph/resolver"
"testing"
"github.com/99designs/gqlgen/client"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/stretchr/testify/require"
)
// API response type
type VerifyResult struct {
Verify bool
NoMatch []string
}
type QueryResponse struct {
Verify VerifyResult
}
// TEST CASE 01: Query with password and rule valid
func TestQueryWithInvalidPasswordAndRule(t *testing.T) {
c := client.New(handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &resolver.Resolver{}})))
query := `{
verify(
password: "TesteSenhaFortee!123&"
rules: [
{rule: "minSize", value: 8},
{rule: "minSpecialChars", value: 2},
{rule: "noRepeted", value: 0},
{rule: "minDigit", value: 4}
]
) {
verify
noMatch
}
}
`
// make request
var resp QueryResponse
c.MustPost(query, &resp)
// check if response is as expected
require.False(t, resp.Verify.Verify)
require.ElementsMatch(t, []string{
"noRepeted", "minDigit",
}, resp.Verify.NoMatch)
}
// TEST CASE 02: Query with a password that does not match the stipulated rules
func TestQueryWithInvalidPassword(t *testing.T) {
c := client.New(handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &resolver.Resolver{}})))
query := `{
verify(
password: "TesteSenhaComum4"
rules: [
{rule: "minSize", value: 4},
{rule: "minSpecialChars", value: 0},
{rule: "noRepeted", value: 0},
{rule: "minDigit", value: 1}
]
) {
verify
noMatch
}
}
`
var resp QueryResponse
c.MustPost(query, &resp)
require.True(t, resp.Verify.Verify)
require.Empty(t, resp.Verify.NoMatch)
}
// TEST CASE 03: Query with invalid rule
func TestQueryWithInvalidRule(t *testing.T) {
c := client.New(handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &resolver.Resolver{}})))
query := `{
verify(
password: "TesteSenhaForte123&"
rules: [{rule: "ruleInvalida", value: 8}]
) {
verify
noMatch
}
}
`
var resp interface{}
// expect errors
require.Panics(t, func() {
c.MustPost(query, &resp)
})
}
// TEST CASE 04: Query with invalid rule value (negative value)
func TestQueryWithInvalidValue(t *testing.T) {
c := client.New(handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &resolver.Resolver{}})))
query := `{
verify(
password: "TesteSenhaForte123&"
rules: [{rule: "ruleInvalida", value: -1}]
) {
verify
noMatch
}
}
`
var resp interface{}
// expect errors
require.Panics(t, func() {
c.MustPost(query, &resp)
})
}