probo/server_test.go

152 lines
3.7 KiB
Go
Raw Normal View History

2022-06-23 11:25:35 +02:00
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
2022-06-28 13:49:35 +02:00
"strings"
2022-06-23 11:25:35 +02:00
"testing"
2022-06-24 18:56:06 +02:00
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/logger"
"git.andreafazzi.eu/andrea/probo/models"
2022-06-23 11:25:35 +02:00
"github.com/remogatto/prettytest"
)
type testSuite struct {
prettytest.Suite
}
type StubTestHubCollectorStore struct {
tests []*models.Quiz
}
2022-06-29 16:08:55 +02:00
func (store *StubTestHubCollectorStore) CreateQuiz(test *client.CreateUpdateQuizRequest) (*models.Quiz, error) {
2022-06-24 18:56:06 +02:00
return nil, nil
2022-06-23 11:25:35 +02:00
}
2022-06-29 16:08:55 +02:00
func (store *StubTestHubCollectorStore) UpdateQuiz(quiz *client.CreateUpdateQuizRequest, id string) (*models.Quiz, error) {
store.tests[0].Question.Text = quiz.Question.Text
return store.tests[0], nil
}
2022-06-23 11:25:35 +02:00
func (store *StubTestHubCollectorStore) ReadAllQuizzes() ([]*models.Quiz, error) {
return store.tests, nil
}
func TestRunner(t *testing.T) {
prettytest.Run(
t,
new(testSuite),
new(integrationTestSuite),
)
}
func (t *testSuite) BeforeAll() {
logger.SetLevel(logger.Disabled)
}
2022-06-29 16:08:55 +02:00
func (t *testSuite) TestReadAllQuizzes() {
2022-06-24 18:56:06 +02:00
expectedResult := &client.ReadAllQuizResponse{
2022-06-29 16:08:55 +02:00
BaseResponse: client.BaseResponse{Status: "success"},
2022-06-23 11:25:35 +02:00
Content: []*models.Quiz{
{
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
},
},
}
store := &StubTestHubCollectorStore{[]*models.Quiz{
{
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
},
}}
2022-06-29 16:08:55 +02:00
server := NewProboCollectorServer(store)
2022-06-23 11:25:35 +02:00
request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
result := getResponse(response.Body)
2022-06-29 16:08:55 +02:00
t.Equal("application/json", response.Header().Get("Content-Type"))
2022-06-23 11:25:35 +02:00
t.True(testsAreEqual(result, expectedResult))
t.Equal(http.StatusOK, response.Code)
}
2022-06-28 13:49:35 +02:00
func (t *testSuite) TestUpdateQuiz() {
expectedResponse := &client.UpdateQuizResponse{
2022-06-29 16:08:55 +02:00
BaseResponse: client.BaseResponse{Status: "success"},
2022-06-28 13:49:35 +02:00
Content: &models.Quiz{
2022-06-29 16:08:55 +02:00
Question: &models.Question{ID: "1", Text: "Updated Question 1"},
2022-06-28 13:49:35 +02:00
Answers: []*models.Answer{{}, {}, {}},
},
}
store := &StubTestHubCollectorStore{[]*models.Quiz{
{
Question: &models.Question{ID: "1", Text: "Question 1"},
Answers: []*models.Answer{{}, {}, {}},
},
}}
2022-06-29 16:08:55 +02:00
server := NewProboCollectorServer(store)
2022-06-28 13:49:35 +02:00
payload := `
{
2022-06-29 16:08:55 +02:00
"question": {"text": "Updated Question 1"},
2022-06-28 13:49:35 +02:00
"answers": [
{"text": "Text of the answer 1", "correct": true},
{"text": "Text of the answer 2"},
{"text": "Text of the answer 3"},
{"text": "Text of the answer 4"}
]
}
`
2022-06-29 16:08:55 +02:00
request, _ := http.NewRequest(http.MethodPut, "/quizzes/update/1", strings.NewReader(payload))
2022-06-28 13:49:35 +02:00
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
result, err := decodeUpdateQuizResponse(response.Body)
t.True(err == nil, fmt.Sprintf("Response should be decoded without errors: %v", err))
if !t.Failed() {
t.True(result.Status == expectedResponse.Status)
t.True(reflect.DeepEqual(result, expectedResponse))
2022-06-29 16:08:55 +02:00
t.Equal("application/json", response.Header().Get("Content-Type"))
t.Equal(http.StatusAccepted, response.Code)
2022-06-28 13:49:35 +02:00
}
}
2022-06-24 18:56:06 +02:00
func getResponse(body io.Reader) (response *client.ReadAllQuizResponse) {
2022-06-23 11:25:35 +02:00
err := json.NewDecoder(body).Decode(&response)
if err != nil {
2022-06-29 16:08:55 +02:00
panic(fmt.Errorf("Unable to parse response from server %q: %v", body, err))
2022-06-23 11:25:35 +02:00
}
return
}
2022-06-28 13:49:35 +02:00
func decodeUpdateQuizResponse(body io.Reader) (response *client.UpdateQuizResponse, err error) {
err = json.NewDecoder(body).Decode(&response)
if err != nil {
return nil, fmt.Errorf("Unable to parse the response %q from the server: %v", body, err)
}
return
}
2022-06-24 18:56:06 +02:00
func testsAreEqual(got, want *client.ReadAllQuizResponse) bool {
2022-06-23 11:25:35 +02:00
return reflect.DeepEqual(got, want)
}