Skip to content

Commit

Permalink
add test Test_saveGradeRequestParsed_UnmarshalJSON checking that DB e…
Browse files Browse the repository at this point in the history
…rrors are handled correctly in saveGradeRequestParsed.UnmarshalJSON()
  • Loading branch information
zenovich committed Dec 30, 2024
1 parent 587f335 commit 7af568a
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions app/api/items/save_grade_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 7af568a

Please sign in to comment.