From 7af568a7d859f8b1d1b0a1dbb26ca2db44f1c553 Mon Sep 17 00:00:00 2001 From: Dmitry Zenovich Date: Mon, 30 Dec 2024 21:59:58 +0300 Subject: [PATCH] add test Test_saveGradeRequestParsed_UnmarshalJSON checking that DB errors are handled correctly in saveGradeRequestParsed.UnmarshalJSON() --- app/api/items/save_grade_test.go | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 app/api/items/save_grade_test.go diff --git a/app/api/items/save_grade_test.go b/app/api/items/save_grade_test.go new file mode 100644 index 000000000..25f756129 --- /dev/null +++ b/app/api/items/save_grade_test.go @@ -0,0 +1,40 @@ +package items + +import ( + "errors" + "fmt" + "regexp" + "testing" + + "github.com/stretchr/testify/assert" + + "github.com/France-ioi/AlgoreaBackend/v2/app/database" + "github.com/France-ioi/AlgoreaBackend/v2/app/payloadstest" + "github.com/France-ioi/AlgoreaBackend/v2/app/token" + "github.com/France-ioi/AlgoreaBackend/v2/app/tokentest" + "github.com/France-ioi/AlgoreaBackend/v2/testhelpers/testoutput" +) + +func Test_saveGradeRequestParsed_UnmarshalJSON(t *testing.T) { + testoutput.SuppressIfPasses(t) + + db, mock := database.NewDBMock() + defer func() { _ = db.Close() }() + + expectedError := errors.New("error") + mock.ExpectQuery("^" + regexp.QuoteMeta("SELECT public_key "+ + "FROM `platforms` JOIN items ON items.platform_id = platforms.id WHERE (items.id = ?) LIMIT 1") + "$"). + WithArgs(901756573345831409).WillReturnError(expectedError) + + r := saveGradeRequestParsed{ + store: database.NewDataStore(db), + publicKey: tokentest.AlgoreaPlatformPublicKeyParsed, + } + assert.PanicsWithError(t, expectedError.Error(), func() { + _ = r.UnmarshalJSON([]byte(fmt.Sprintf(`{"score_token": %q, "answer_token": %q}`, + token.Generate(payloadstest.ScorePayloadFromGrader, tokentest.AlgoreaPlatformPrivateKeyParsed), + token.Generate(payloadstest.AnswerPayloadFromAlgoreaPlatform, tokentest.AlgoreaPlatformPrivateKeyParsed), + ))) + }) + assert.NoError(t, mock.ExpectationsWereMet()) +}