probo/server_integration_test.go
2022-06-29 16:08:55 +02:00

201 lines
5.5 KiB
Go

package main
import (
"encoding/json"
"fmt"
"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 := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore(
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, "Response should be decoded properly")
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)
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, "Response should be decoded properly")
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)
}
}
func (t *integrationTestSuite) TestQuizCreateAndUpdate() {
server := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore(
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, "Response should be decoded properly")
if !t.Failed() {
payload = `
{
"question": {"text": "Updated Question 1"},
"answers": [
{"text": "Text of the answer 1"},
{"text": "Text of the answer 2"},
{"text": "Text of the answer 3", "correct": true},
{"text": "Text of the answer 4"}
]
}
`
updateQuizResponse, err := t.updateQuiz(server, payload, createQuizResponse.Content.ID)
t.True(err == nil, "Response should be decoded properly")
if !t.Failed() {
t.Equal("Updated Question 1", updateQuizResponse.Content.Question.Text)
t.Equal("Text of the answer 3", updateQuizResponse.Content.Correct.Text)
}
}
}
func (t *integrationTestSuite) TestUpdateNotExistentQuiz() {
server := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore(
sha256.NewDefault256Hasher(sha256.DefaultSHA256HashingFn),
),
)
payload := ""
r, err := t.updateQuiz(server, payload, "1234")
t.True(err == nil, fmt.Sprintf("The operation should not return an error: %v", err))
t.Equal("error", r.Status)
}
func (t *integrationTestSuite) TestCatchDuplicateQuiz() {
server := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore(
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, fmt.Sprintf("Create quiz should not raise an error: %v", err))
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 *ProboCollectorServer, payload string) (*client.CreateQuizResponse, error) {
request, _ := http.NewRequest(http.MethodPost, "/quizzes/create", 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) updateQuiz(server *ProboCollectorServer, payload string, id string) (*client.UpdateQuizResponse, error) {
request, _ := http.NewRequest(http.MethodPut, "/quizzes/update/"+id, strings.NewReader(payload))
response := httptest.NewRecorder()
server.ServeHTTP(response, request)
decodedResponse := new(client.UpdateQuizResponse)
err := json.Unmarshal(response.Body.Bytes(), decodedResponse)
if err != nil {
return decodedResponse, err
}
return decodedResponse, err
}
func (t *integrationTestSuite) readAllQuiz(server *ProboCollectorServer) (*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
}