summaryrefslogtreecommitdiff
path: root/handlers_test.go
blob: db97502607f6bf66f568a119138c0e3e72cd7e14 (plain)
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
package main

import (
	"bytes"
	"database/sql"
	"net/http"
	"net/http/httptest"
	"testing"
)

type Handler func(db *sql.DB) http.Handler

type httpCase struct {
	name   string
	method string
	input  []byte
	output []byte
	status int
}

func testHandler(
	t *testing.T,
	handler Handler,
	cases []httpCase,
	setup func(t *testing.T) *sql.DB,
) {
	for _, c := range cases {
		t.Run(c.name, func(t *testing.T) {
			db := setup(t)

			in := bytes.NewReader(c.input)

			rr := httptest.NewRecorder()

			req, err := http.NewRequest("POST", "/", in)
			if err != nil {
				t.Fatal(err)
			}

			handler(db).ServeHTTP(rr, req)

			if rr.Code != c.status {
				t.Errorf("handler returned wrong status code: got %v want %v",
					rr.Code, c.status)
			}

			if rr.Body.String() != string(c.output) {
				t.Errorf("handler returned unexpected body: got %v want %v",
					rr.Body.String(), string(c.output))
			}
		})
	}
}

func TestHTTPVersion(t *testing.T) {
	cases := []httpCase{
		{"basic", "GET", []byte(``), []byte(versionTxt), http.StatusOK},
	}

	setup := func(t *testing.T) *sql.DB {
		return nil
	}

	testHandler(t, version, cases, setup)
}

func TestHTTPRegister(t *testing.T) {
	cases := []httpCase{
		{"basic", "POST", []byte(`{"username":"testuser","password":"mypass"}`), []byte(""), http.StatusOK},
		{"missing-password", "POST", []byte(`{"username":"testuser"}`), []byte(""), http.StatusBadRequest},
		{"missing-username", "POST", []byte(`{"password":"mypass"}`), []byte(""), http.StatusBadRequest},
		{"unknown-field", "POST", []byte(`{"username":"testuser","password":"mypass","foo":"bar"}`), []byte(""), http.StatusBadRequest},
		{"invalid-json", "POST", []byte(`{username":"testuser","password":"mypass"}`), []byte(""), http.StatusBadRequest},
	}

	setup := func(t *testing.T) *sql.DB {
		db, _ := emptyDB(t)
		err := MigrateToLatest(db)
		if err != nil {
			t.Fatalf("Could not migrate test database: %v", err)
		}
		return db
	}

	testHandler(t, ApiRegistration, cases, setup)
}

func TestHTTPAuthenticate(t *testing.T) {
	cases := []httpCase{
		{"basic", "POST", []byte(`{"username":"testuser","password":"mypass"}`), []byte(""), http.StatusOK},
		{"missing-password", "POST", []byte(`{"username":"testuser"}`), []byte(""), http.StatusBadRequest},
		{"missing-username", "POST", []byte(`{"password":"mypass"}`), []byte(""), http.StatusBadRequest},
		{"unknown-field", "POST", []byte(`{"username":"testuser","password":"mypass","foo":"bar"}`), []byte(""), http.StatusBadRequest},
		{"invalid-json", "POST", []byte(`{username":"testuser","password":"mypass"}`), []byte(""), http.StatusBadRequest},
		{"wrong-username", "POST", []byte(`{"username":"wronguser","password":"mypass"}`), []byte(""), http.StatusUnauthorized},
		{"wrong-password", "POST", []byte(`{"username":"testuser","password":"wrongpass"}`), []byte(""), http.StatusUnauthorized},
	}

	setup := func(t *testing.T) *sql.DB {
		db, _ := emptyDB(t)

		err := MigrateToLatest(db)
		if err != nil {
			t.Fatalf("Could not migrate test database: %v", err)
		}

		err = Register(db, "testuser", "mypass")
		if err != nil {
			t.Fatalf("Failed to register test user: %v", err)
		}

		return db
	}

	testHandler(t, ApiAuthentication, cases, setup)
}