Commit 0dd6ddb8 authored by acud's avatar acud Committed by GitHub

api: dont accept two soc's with the same address (#1256)

parent 62c576e7
......@@ -103,8 +103,22 @@ func (s *server) socUploadHandler(w http.ResponseWriter, r *http.Request) {
return
}
ctx := r.Context()
has, err := s.storer.Has(ctx, chunk.Address())
if err != nil {
s.logger.Debugf("soc upload: store has: %v", err)
s.logger.Error("soc upload: store has")
jsonhttp.InternalServerError(w, "storage error")
return
}
if has {
s.logger.Error("soc upload: chunk already exists")
jsonhttp.Conflict(w, "chunk already exists")
return
}
_, err = s.storer.Put(ctx, requestModePut(r), chunk)
if err != nil {
s.logger.Debugf("soc upload: chunk write error: %v", err)
......
......@@ -124,6 +124,32 @@ func TestSoc(t *testing.T) {
t.Fatal("data retrieved doesnt match uploaded content")
}
})
t.Run("already exists", func(t *testing.T) {
s, owner, payload := mockSoc(t)
id := make([]byte, soc.IdSize)
sig := s.Signature()
addr, err := s.Address()
if err != nil {
t.Fatal(err)
}
jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(owner), hex.EncodeToString(id), hex.EncodeToString(sig)), http.StatusCreated,
jsonhttptest.WithRequestBody(bytes.NewReader(payload)),
jsonhttptest.WithExpectedJSONResponse(api.SocPostResponse{
Reference: addr,
}),
)
jsonhttptest.Request(t, client, http.MethodPost, socResource(hex.EncodeToString(owner), hex.EncodeToString(id), hex.EncodeToString(sig)), http.StatusConflict,
jsonhttptest.WithRequestBody(bytes.NewReader(payload)),
jsonhttptest.WithExpectedJSONResponse(
jsonhttp.StatusResponse{
Message: "chunk already exists",
Code: http.StatusConflict,
}),
)
})
}
// returns a valid, mocked SOC
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment