aboutsummaryrefslogtreecommitdiff
path: root/api/cmd/main_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'api/cmd/main_test.go')
-rw-r--r--api/cmd/main_test.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/api/cmd/main_test.go b/api/cmd/main_test.go
new file mode 100644
index 0000000..8d0df8d
--- /dev/null
+++ b/api/cmd/main_test.go
@@ -0,0 +1,56 @@
1package main
2
3import (
4 "encoding/json"
5 "log"
6 "net/http"
7 "net/http/httptest"
8 "testing"
9
10 "github.com/spf13/viper"
11 "github.com/stretchr/testify/assert"
12)
13
14func getTestUserCredentials() (string, string) {
15 viper.SetConfigName(".env")
16 viper.AddConfigPath(".")
17 err := viper.ReadInConfig()
18 if err != nil {
19 log.Fatalf("Error while reading config file %s", err)
20 }
21
22 testUser := viper.GetString("TEST_USER")
23 testPass := viper.GetString("TEST_PASS")
24 return testUser, testPass
25}
26
27func TestAuthRoute(t *testing.T) {
28 router := setupRouter()
29
30 username, password := getTestUserCredentials()
31
32 w := httptest.NewRecorder()
33 req, _ := http.NewRequest("POST", "/api/v1/auth", nil)
34 req.SetBasicAuth(username, password)
35 router.ServeHTTP(w, req)
36
37 assert.Equal(t, http.StatusOK, w.Code, "response should return a 200 code")
38
39 var response map[string]interface{}
40 _ = json.Unmarshal(w.Body.Bytes(), &response)
41 _, exists := response["token"]
42 assert.True(t, exists, "response should return a token")
43}
44
45func TestAuthRouteFailure(t *testing.T) {
46 router := setupRouter()
47
48 w := httptest.NewRecorder()
49 req, _ := http.NewRequest("POST", "/api/v1/auth", nil)
50 req.SetBasicAuth("asdf", "asdf")
51 router.ServeHTTP(w, req)
52
53 assert.Equal(t, http.StatusUnauthorized, w.Code, "should return a 401 code")
54}
55
56func Test \ No newline at end of file