probo/server_integration_test.go

124 lines
3.4 KiB
Go
Raw Normal View History

2022-06-23 11:25:35 +02:00
package main
import (
"encoding/json"
"net/http"
"net/http/httptest"
2022-06-24 18:56:06 +02:00
"reflect"
2022-06-23 11:25:35 +02:00
"strings"
2022-06-24 18:56:06 +02:00
"git.andreafazzi.eu/andrea/probo/client"
"git.andreafazzi.eu/andrea/probo/hasher/sha256"
"git.andreafazzi.eu/andrea/probo/store/memory"
2022-06-23 11:25:35 +02:00
"github.com/remogatto/prettytest"
)
type integrationTestSuite struct {
prettytest.Suite
}
func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
2022-06-24 18:56:06 +02:00
server := NewQuizHubCollectorServer(
memory.NewMemoryQuizHubCollectorStore(
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
),
)
2022-06-23 11:25:35 +02:00
// POST a new question using a JSON payload
payload := `
{
"question": {"text": "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"}
]
}
`
2022-06-24 18:56:06 +02:00
createQuizResponse, err := t.createQuiz(server, payload)
2022-06-28 13:49:35 +02:00
t.True(err == nil, "Response should be decoded properly")
2022-06-24 18:56:06 +02:00
2022-06-28 13:49:35 +02:00
if !t.Failed() {
t.Equal("success", createQuizResponse.Status)
t.Equal("Question 1", createQuizResponse.Content.Question.Text)
t.Equal("Text of the answer 1", createQuizResponse.Content.Answers[0].Text)
t.Equal("Text of the answer 1", createQuizResponse.Content.Correct.Text)
2022-06-24 18:56:06 +02:00
2022-06-28 13:49:35 +02:00
t.True(createQuizResponse.Content.ID != "", "Test ID should not be empty")
t.True(createQuizResponse.Content.Question.ID != "", "Question ID should not be empty")
t.True(createQuizResponse.Content.Answers[0].ID != "", "Answer ID should not be empty")
}
2022-06-24 18:56:06 +02:00
readAllQuizResponse, err := t.readAllQuiz(server)
2022-06-28 13:49:35 +02:00
t.True(err == nil, "Response should be decoded properly")
2022-06-24 18:56:06 +02:00
2022-06-28 13:49:35 +02:00
if !t.Failed() {
t.True(len(readAllQuizResponse.Content) == 1, "Length of returned tests should be 1")
t.Equal("Question 1", readAllQuizResponse.Content[0].Question.Text)
}
2022-06-24 18:56:06 +02:00
}
func (t *integrationTestSuite) TestCatchDuplicateQuiz() {
server := NewQuizHubCollectorServer(
memory.NewMemoryQuizHubCollectorStore(
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
),
)
// POST a new question using a JSON payload
payload := `
{
"question": {"text": "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"}
]
}
`
quiz1, err := t.createQuiz(server, payload)
2022-06-27 15:11:37 +02:00
2022-06-24 18:56:06 +02:00
t.True(err == nil)
2022-06-27 15:11:37 +02:00
2022-06-24 18:56:06 +02:00
quiz2, err := t.createQuiz(server, payload)
2022-06-27 15:11:37 +02:00
t.True(err == nil, "Quizzes are duplicated, but the API should not return an error")
t.True(reflect.DeepEqual(quiz1, quiz2), "Quizzes shold be exactly the same")
2022-06-24 18:56:06 +02:00
}
func (t *integrationTestSuite) createQuiz(server *QuizHubCollectorServer, payload string) (*client.CreateQuizResponse, error) {
2022-06-23 11:25:35 +02:00
request, _ := http.NewRequest(http.MethodPost, "/quizzes", strings.NewReader(payload))
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
2022-06-24 18:56:06 +02:00
decodedResponse := new(client.CreateQuizResponse)
2022-06-23 11:25:35 +02:00
2022-06-24 18:56:06 +02:00
err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
2022-06-23 11:25:35 +02:00
if err != nil {
2022-06-24 18:56:06 +02:00
return nil, err
2022-06-23 11:25:35 +02:00
}
2022-06-24 18:56:06 +02:00
return decodedResponse, err
}
2022-06-23 11:25:35 +02:00
2022-06-24 18:56:06 +02:00
func (t *integrationTestSuite) readAllQuiz(server *QuizHubCollectorServer) (*client.ReadAllQuizResponse, error) {
request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
response := httptest.NewRecorder()
2022-06-23 11:25:35 +02:00
server.ServeHTTP(response, request)
2022-06-24 18:56:06 +02:00
decodedResponse := new(client.ReadAllQuizResponse)
2022-06-23 11:25:35 +02:00
2022-06-24 18:56:06 +02:00
err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
2022-06-23 11:25:35 +02:00
if err != nil {
2022-06-24 18:56:06 +02:00
return nil, err
2022-06-23 11:25:35 +02:00
}
2022-06-24 18:56:06 +02:00
return decodedResponse, err
2022-06-23 11:25:35 +02:00
}