probo/server_integration_test.go

202 lines
5.5 KiB
Go
Raw Normal View History

2022-06-23 11:25:35 +02:00
package main
import (
"encoding/json"
2022-06-29 16:08:55 +02:00
"fmt"
2022-06-23 11:25:35 +02:00
"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-29 16:08:55 +02:00
server := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore(
2022-06-24 18:56:06 +02:00
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
}
2022-06-29 16:08:55 +02:00
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)
}
2022-06-24 18:56:06 +02:00
func (t *integrationTestSuite) TestCatchDuplicateQuiz() {
2022-06-29 16:08:55 +02:00
server := NewProboCollectorServer(
memory.NewMemoryProboCollectorStore(
2022-06-24 18:56:06 +02:00
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-29 16:08:55 +02:00
t.True(err == nil, fmt.Sprintf("Create quiz should not raise an error: %v", err))
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
}
2022-06-29 16:08:55 +02:00
func (t *integrationTestSuite) createQuiz(server *ProboCollectorServer, payload string) (*client.CreateQuizResponse, error) {
request, _ := http.NewRequest(http.MethodPost, "/quizzes/create", strings.NewReader(payload))
2022-06-23 11:25:35 +02:00
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-29 16:08:55 +02:00
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) {
2022-06-24 18:56:06 +02:00
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
}