forked from exercism/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
175 lines (141 loc) · 4.22 KB
/
api_test.go
File metadata and controls
175 lines (141 loc) · 4.22 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package exercism
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
)
var assignmentsJson = `
{
"assignments": [
{
"track": "ruby",
"slug": "bob",
"readme": "Readme text",
"test_file": "bob_test.rb",
"tests": "Tests Text"
}
]
}
`
var fetchHandler = func(rw http.ResponseWriter, r *http.Request) {
r.ParseForm()
apiKey := r.Form.Get("key")
if r.URL.Path != "/api/v1/user/assignments/current" {
fmt.Println("Not found")
rw.WriteHeader(http.StatusNotFound)
return
}
if apiKey != "myApiKey" {
rw.WriteHeader(http.StatusForbidden)
fmt.Fprintf(rw, `{"error": "Unable to identify user"}`)
return
}
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, assignmentsJson)
}
func TestFetchWithKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(fetchHandler))
assignments, err := FetchAssignments(server.URL, "/api/v1/user/assignments/current", "myApiKey")
assert.NoError(t, err)
assert.Equal(t, len(assignments), 1)
assert.Equal(t, assignments[0].Track, "ruby")
assert.Equal(t, assignments[0].Slug, "bob")
assert.Equal(t, assignments[0].Readme, "Readme text")
assert.Equal(t, assignments[0].TestFile, "bob_test.rb")
assert.Equal(t, assignments[0].Tests, "Tests Text")
server.Close()
}
func TestFetchWithIncorrectKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(fetchHandler))
assignments, err := FetchAssignments(server.URL, "/api/v1/user/assignments/current", "myWrongApiKey")
assert.Error(t, err)
assert.Equal(t, len(assignments), 0)
server.Close()
}
var submitHandler = func(rw http.ResponseWriter, r *http.Request) {
pathMatches := r.URL.Path == "/api/v1/user/assignments"
methodMatches := r.Method == "POST"
if !(pathMatches && methodMatches) {
rw.WriteHeader(http.StatusNotFound)
return
}
userAgentMatches := r.Header.Get("User-Agent") == fmt.Sprintf("github.com/kytrinyx/exercism CLI v%s", VERSION)
if !userAgentMatches {
fmt.Printf("User agent mismatch: %s\n", r.Header.Get("User-Agent"))
rw.WriteHeader(http.StatusInternalServerError)
return
}
body, err := ioutil.ReadAll(r.Body)
r.Body.Close()
if err != nil {
rw.WriteHeader(http.StatusInternalServerError)
fmt.Printf("Reading body error: %s\n", err)
return
}
type Submission struct {
Key string
Code string
Path string
}
submission := Submission{}
err = json.Unmarshal(body, &submission)
if err != nil {
fmt.Printf("Unmarshalling error: %s, Body: %s\n", err, body)
rw.WriteHeader(http.StatusInternalServerError)
return
}
if submission.Key != "myApiKey" {
rw.WriteHeader(http.StatusForbidden)
rw.Header().Set("Content-Type", "application/json")
fmt.Fprintf(rw, `{"error": "Unable to identify user"}`)
return
}
code := submission.Code
filePath := submission.Path
codeMatches := string(code) == "My source code\n"
filePathMatches := filePath == "ruby/bob/bob.rb"
if !filePathMatches {
fmt.Printf("FilePathMismatch: File Path: %s\n", filePath)
rw.WriteHeader(http.StatusBadRequest)
return
}
if !codeMatches {
fmt.Printf("Code Mismatch: Code: %v\n", code)
rw.WriteHeader(http.StatusBadRequest)
return
}
rw.WriteHeader(http.StatusCreated)
rw.Header().Set("Content-Type", "application/json")
submitJson := `
{
"status":"saved",
"language":"ruby",
"exercise":"bob",
"submission_path":"/username/ruby/bob"
}
`
fmt.Fprintf(rw, submitJson)
}
func TestSubmitWithKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(submitHandler))
defer server.Close()
var code = []byte("My source code\n")
response, err := SubmitAssignment(server.URL, "myApiKey", "ruby/bob/bob.rb", code)
assert.NoError(t, err)
assert.Equal(t, response.Status, "saved")
assert.Equal(t, response.Language, "ruby")
assert.Equal(t, response.Exercise, "bob")
assert.Equal(t, response.SubmissionPath, "/username/ruby/bob")
}
func TestSubmitWithIncorrectKey(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(submitHandler))
defer server.Close()
var code = []byte("My source code\n")
response, err := SubmitAssignment(server.URL, "myWrongApiKey", "ruby/bob/bob.rb", code)
assert.Error(t, err)
assert.Nil(t, response)
}