123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201 |
- 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
- }
|