package main import ( "encoding/json" "fmt" "io" "net/http" "net/http/httptest" "reflect" "strings" "testing" "git.andreafazzi.eu/andrea/probo/client" "git.andreafazzi.eu/andrea/probo/logger" "git.andreafazzi.eu/andrea/probo/models" "github.com/remogatto/prettytest" ) type testSuite struct { prettytest.Suite } type StubTestHubCollectorStore struct { tests []*models.Quiz } func (store *StubTestHubCollectorStore) CreateQuiz(test *client.CreateUpdateQuizRequest) (*models.Quiz, error) { return nil, nil } 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 } 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) } func (t *testSuite) TestReadAllQuizzes() { expectedResult := &client.ReadAllQuizResponse{ BaseResponse: client.BaseResponse{Status: "success"}, 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{{}, {}, {}}, }, }} server := NewProboCollectorServer(store) request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil) response := httptest.NewRecorder() server.ServeHTTP(response, request) result := getResponse(response.Body) t.Equal("application/json", response.Header().Get("Content-Type")) t.True(testsAreEqual(result, expectedResult)) t.Equal(http.StatusOK, response.Code) } func (t *testSuite) TestUpdateQuiz() { expectedResponse := &client.UpdateQuizResponse{ BaseResponse: client.BaseResponse{Status: "success"}, Content: &models.Quiz{ Question: &models.Question{ID: "1", Text: "Updated Question 1"}, Answers: []*models.Answer{{}, {}, {}}, }, } store := &StubTestHubCollectorStore{[]*models.Quiz{ { Question: &models.Question{ID: "1", Text: "Question 1"}, Answers: []*models.Answer{{}, {}, {}}, }, }} server := NewProboCollectorServer(store) payload := ` { "question": {"text": "Updated Question 1"}, "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"} ] } ` request, _ := http.NewRequest(http.MethodPut, "/quizzes/update/1", strings.NewReader(payload)) 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)) t.Equal("application/json", response.Header().Get("Content-Type")) t.Equal(http.StatusAccepted, response.Code) } } func getResponse(body io.Reader) (response *client.ReadAllQuizResponse) { err := json.NewDecoder(body).Decode(&response) if err != nil { panic(fmt.Errorf("Unable to parse response from server %q: %v", body, err)) } return } 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 } func testsAreEqual(got, want *client.ReadAllQuizResponse) bool { return reflect.DeepEqual(got, want) }