Skip to content

Commit 4fc9071

Browse files
committed
fix: return apt Gone status code and error message when OTP no longer exists in the store.
1 parent 277bf58 commit 4fc9071

3 files changed

Lines changed: 6 additions & 6 deletions

File tree

cmd/otpgateway/handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ func handleVerifyOTP(w http.ResponseWriter, r *http.Request) {
318318
if err != nil {
319319
code := http.StatusBadRequest
320320
if err == store.ErrNotExist {
321-
sendErrorResponse(w, err.Error(), code, nil)
321+
sendErrorResponse(w, err.Error(), http.StatusGone, nil)
322322
return
323323
}
324324

cmd/otpgateway/handlers_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -234,15 +234,15 @@ func TestCheckOTP(t *testing.T) {
234234

235235
// Check it again. Should be deleted.
236236
r = testRequest(t, http.MethodPost, "/api/otp/"+dummyOTPID, cp, &data)
237-
assert.NotEqual(t, http.StatusOK, r.StatusCode, "OTP didn't get deleted on verification")
237+
assert.Equal(t, http.StatusGone, r.StatusCode, "OTP didn't get deleted on verification")
238238

239-
// Check non-existent OTP, should not return 200.
239+
// Check non-existent OTP, should return 410 Gone.
240240
r = testRequest(t, http.MethodPost, "/api/otp/abc123", cp, &data)
241-
assert.NotEqual(t, http.StatusOK, r.StatusCode, "non-existent OTP didn't return 400")
241+
assert.Equal(t, http.StatusGone, r.StatusCode, "non-existent OTP didn't return 410")
242242

243243
// Check non-existent OTP, should return store.ErrNotExist error message.
244244
_ = testRequest(t, http.MethodPost, "/api/otp/abc123", cp, &out)
245-
assert.Equal(t, "the OTP does not exist", out.Message, "non-existent OTP passed")
245+
assert.Equal(t, "OTP has expired or doesn't exist", out.Message, "non-existent OTP passed")
246246
}
247247

248248
func TestCheckOTPAttempts(t *testing.T) {

internal/store/store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88

99
// ErrNotExist is thrown when an OTP (requested by namespace / ID)
1010
// does not exist.
11-
var ErrNotExist = errors.New("the OTP does not exist")
11+
var ErrNotExist = errors.New("OTP has expired or doesn't exist")
1212

1313
const (
1414
CounterAttempts = "attempts"

0 commit comments

Comments
 (0)