| 1 |
// Copyright 2023 The Archivista Contributors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package api_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"net/http" |
| 20 |
"net/http/httptest" |
| 21 |
"testing" |
| 22 |
|
| 23 |
"github.com/in-toto/archivista/pkg/api" |
| 24 |
"github.com/stretchr/testify/suite" |
| 25 |
) |
| 26 |
|
| 27 |
// Test Suite: UT APIStore |
| 28 |
type UTAPIGraphQLSuite struct { |
| 29 |
suite.Suite |
| 30 |
} |
| 31 |
|
| 32 |
func TestAPIGraphQLSuite(t *testing.T) { |
| 33 |
suite.Run(t, new(UTAPIGraphQLSuite)) |
| 34 |
} |
| 35 |
|
| 36 |
func (ut *UTAPIGraphQLSuite) Test_Store() { |
| 37 |
|
| 38 |
testServer := httptest.NewServer( |
| 39 |
http.HandlerFunc( |
| 40 |
func(w http.ResponseWriter, r *http.Request) { |
| 41 |
w.WriteHeader(http.StatusOK) |
| 42 |
_, err := w.Write([]byte(`{"data": {"data": "test"}}`)) |
| 43 |
if err != nil { |
| 44 |
ut.FailNow(err.Error()) |
| 45 |
} |
| 46 |
}, |
| 47 |
), |
| 48 |
) |
| 49 |
defer testServer.Close() |
| 50 |
ctx := context.TODO() |
| 51 |
|
| 52 |
type testSubjectVar struct { |
| 53 |
Gitoid string `json:"gitoid"` |
| 54 |
} |
| 55 |
|
| 56 |
type testSubjectResult struct { |
| 57 |
Data string `json:"data"` |
| 58 |
} |
| 59 |
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) |
| 60 |
ut.NoError(err) |
| 61 |
ut.Equal(testSubjectResult{Data: "test"}, result) |
| 62 |
} |
| 63 |
|
| 64 |
func (ut *UTAPIGraphQLSuite) Test_Store_NoServer() { |
| 65 |
|
| 66 |
ctx := context.TODO() |
| 67 |
|
| 68 |
type testSubjectVar struct { |
| 69 |
Gitoid string `json:"gitoid"` |
| 70 |
} |
| 71 |
|
| 72 |
type testSubjectResult struct { |
| 73 |
Data string `json:"data"` |
| 74 |
} |
| 75 |
result, err := api.GraphQlQuery[testSubjectResult](ctx, "http://invalid-archivista", `query`, testSubjectVar{Gitoid: "test_Gitoid"}) |
| 76 |
ut.Error(err) |
| 77 |
ut.Equal(testSubjectResult{Data: ""}, result) |
| 78 |
} |
| 79 |
|
| 80 |
func (ut *UTAPIGraphQLSuite) Test_Store_BadStatusCode_NoMsg() { |
| 81 |
|
| 82 |
testServer := httptest.NewServer( |
| 83 |
http.HandlerFunc( |
| 84 |
func(w http.ResponseWriter, r *http.Request) { |
| 85 |
w.WriteHeader(http.StatusInternalServerError) |
| 86 |
}, |
| 87 |
), |
| 88 |
) |
| 89 |
defer testServer.Close() |
| 90 |
ctx := context.TODO() |
| 91 |
|
| 92 |
type testSubjectVar struct { |
| 93 |
Gitoid string `json:"gitoid"` |
| 94 |
} |
| 95 |
|
| 96 |
type testSubjectResult struct { |
| 97 |
Data string `json:"data"` |
| 98 |
} |
| 99 |
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) |
| 100 |
ut.Error(err) |
| 101 |
ut.Equal(testSubjectResult{Data: ""}, result) |
| 102 |
} |
| 103 |
|
| 104 |
func (ut *UTAPIGraphQLSuite) Test_Store_InvalidData() { |
| 105 |
|
| 106 |
testServer := httptest.NewServer( |
| 107 |
http.HandlerFunc( |
| 108 |
func(w http.ResponseWriter, r *http.Request) { |
| 109 |
w.WriteHeader(http.StatusOK) |
| 110 |
_, err := w.Write([]byte(``)) |
| 111 |
if err != nil { |
| 112 |
ut.FailNow(err.Error()) |
| 113 |
} |
| 114 |
}, |
| 115 |
), |
| 116 |
) |
| 117 |
defer testServer.Close() |
| 118 |
ctx := context.TODO() |
| 119 |
|
| 120 |
type testSubjectVar struct { |
| 121 |
Gitoid string `json:"gitoid"` |
| 122 |
} |
| 123 |
|
| 124 |
type testSubjectResult struct { |
| 125 |
Data string `json:"data"` |
| 126 |
} |
| 127 |
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) |
| 128 |
ut.Error(err) |
| 129 |
ut.Equal(testSubjectResult{Data: ""}, result) |
| 130 |
} |
| 131 |
|
| 132 |
func (ut *UTAPIGraphQLSuite) Test_Store_QLReponseWithErrors() { |
| 133 |
|
| 134 |
testServer := httptest.NewServer( |
| 135 |
http.HandlerFunc( |
| 136 |
func(w http.ResponseWriter, r *http.Request) { |
| 137 |
w.WriteHeader(http.StatusOK) |
| 138 |
_, err := w.Write([]byte(`{"data": {"data": "test"}, "errors": [{"message": "test_error"}]}}`)) |
| 139 |
if err != nil { |
| 140 |
ut.FailNow(err.Error()) |
| 141 |
} |
| 142 |
}, |
| 143 |
), |
| 144 |
) |
| 145 |
defer testServer.Close() |
| 146 |
ctx := context.TODO() |
| 147 |
|
| 148 |
type testSubjectVar struct { |
| 149 |
Gitoid string `json:"gitoid"` |
| 150 |
} |
| 151 |
|
| 152 |
type testSubjectResult struct { |
| 153 |
Data string `json:"data"` |
| 154 |
Errors string `json:"errors"` |
| 155 |
} |
| 156 |
|
| 157 |
result, err := api.GraphQlQuery[testSubjectResult](ctx, testServer.URL, `query`, testSubjectVar{Gitoid: "test_Gitoid"}) |
| 158 |
ut.Error(err) |
| 159 |
ut.EqualError(err, "graph ql query failed: [{test_error}]") |
| 160 |
ut.Equal(testSubjectResult{Data: ""}, result) |
| 161 |
} |