-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_collaborator_handler_test.go
75 lines (60 loc) · 1.62 KB
/
add_collaborator_handler_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
package main_test
import (
"bytes"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/blamewarrior/collaborators/blamewarrior"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
main "github.com/blamewarrior/collaborators"
)
func TestAddCollaboratorHandler(t *testing.T) {
results := []struct {
Owner string
Name string
ResponseCode int
ResponseBody string
}{
{
Owner: "",
Name: "",
ResponseCode: http.StatusBadRequest,
ResponseBody: "Incorrect full name\n",
},
{
Owner: "blamewarrior",
Name: "test_add_account",
ResponseCode: http.StatusCreated,
ResponseBody: "",
},
}
for _, result := range results {
db, teardown := setupTestDBConn()
_, err := db.Exec("TRUNCATE repositories, collaboration, accounts")
require.NoError(t, err)
_, err = db.Exec(blamewarrior.CreateRepositoryQuery, fmt.Sprintf("%s/%s", result.Owner, result.Name))
require.NoError(t, err)
req, err := http.NewRequest("POST", "/collaborators?:username="+result.Owner+"&:repo="+result.Name, bytes.NewBufferString(addCollaboratorRequestBody))
require.NoError(t, err)
w := httptest.NewRecorder()
collaboration := blamewarrior.NewCollaborationService()
handler := main.NewAddCollaboratorHandler("blamewarrior.com", db, collaboration)
handler.ServeHTTP(w, req)
assert.Equal(t, result.ResponseCode, w.Code)
assert.Equal(t, result.ResponseBody, fmt.Sprintf("%v", w.Body))
teardown()
}
}
const (
addCollaboratorRequestBody = `
{
"uid": 1345,
"login": "blamewarrior",
"permissions": {
"admin": true
}
}
`
)