119 lines
3.3 KiB
Go
119 lines
3.3 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"git.andreafazzi.eu/andrea/probo/client"
|
|
"git.andreafazzi.eu/andrea/probo/hasher/sha256"
|
|
"git.andreafazzi.eu/andrea/probo/store/memory"
|
|
"github.com/remogatto/prettytest"
|
|
)
|
|
|
|
type integrationTestSuite struct {
|
|
prettytest.Suite
|
|
}
|
|
|
|
func (t *integrationTestSuite) TestQuizCreateAndReadAll() {
|
|
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"}
|
|
]
|
|
}
|
|
`
|
|
createQuizResponse, err := t.createQuiz(server, payload)
|
|
t.True(err == nil)
|
|
|
|
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)
|
|
|
|
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")
|
|
|
|
readAllQuizResponse, err := t.readAllQuiz(server)
|
|
t.True(err == nil)
|
|
|
|
t.True(len(readAllQuizResponse.Content) == 1, "Length of returned tests should be 1")
|
|
t.Equal("Question 1", readAllQuizResponse.Content[0].Question.Text)
|
|
}
|
|
|
|
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)
|
|
|
|
t.True(err == nil)
|
|
|
|
quiz2, err := t.createQuiz(server, payload)
|
|
|
|
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")
|
|
}
|
|
|
|
func (t *integrationTestSuite) createQuiz(server *QuizHubCollectorServer, payload string) (*client.CreateQuizResponse, error) {
|
|
request, _ := http.NewRequest(http.MethodPost, "/quizzes", strings.NewReader(payload))
|
|
response := httptest.NewRecorder()
|
|
|
|
server.ServeHTTP(response, request)
|
|
|
|
decodedResponse := new(client.CreateQuizResponse)
|
|
|
|
err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return decodedResponse, err
|
|
}
|
|
|
|
func (t *integrationTestSuite) readAllQuiz(server *QuizHubCollectorServer) (*client.ReadAllQuizResponse, error) {
|
|
request, _ := http.NewRequest(http.MethodGet, "/quizzes", nil)
|
|
response := httptest.NewRecorder()
|
|
|
|
server.ServeHTTP(response, request)
|
|
|
|
decodedResponse := new(client.ReadAllQuizResponse)
|
|
|
|
err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return decodedResponse, err
|
|
}
|