Skip to content

Commit

Permalink
fix encoding using processId marshaling
Browse files Browse the repository at this point in the history
  • Loading branch information
jordipainan committed Nov 28, 2023
1 parent 7fc144c commit 244ed50
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
20 changes: 10 additions & 10 deletions api/elections.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,14 +679,14 @@ func (a *API) electionFilterPaginatedHandler(msg *apirest.APIdata, ctx *httprout

// nextElectionIDHandler
//
// @Summary Get next election ID
// @Description nextElectionIDHandler
// @Tags Elections
// @Accept json
// @Produce json
// @Param transaction body NextElectionID true "OrganizationID, CensusOrigin and EnvelopeType"
// @Success 200 {object} object{electionID=string}
// @Router /elections/id [post]
// @Summary Get next election ID
// @Description nextElectionIDHandler
// @Tags Elections
// @Accept json
// @Produce json
// @Param transaction body NextElectionID true "OrganizationID, CensusOrigin and EnvelopeType"
// @Success 200 {object} object{electionID=string}
// @Router /elections/id [post]
func (a *API) nextElectionIDHandler(msg *apirest.APIdata, ctx *httprouter.HTTPContext) error {
body := &NextElectionID{}
if err := json.Unmarshal(msg.Data, body); err != nil {
Expand All @@ -712,9 +712,9 @@ func (a *API) nextElectionIDHandler(msg *apirest.APIdata, ctx *httprouter.HTTPCo
}

data, err := json.Marshal(struct {
ElectionID string `json:"electionID"`
ElectionID []byte `json:"electionID"`
}{
ElectionID: pid.String(),
ElectionID: pid.Marshal(),
})
if err != nil {
return ErrMarshalingServerJSONFailed.WithErr(err)
Expand Down
35 changes: 28 additions & 7 deletions test/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,8 @@ func TestAPINextElectionID(t *testing.T) {
api.WalletHandler,
)

c := testutil.NewTestHTTPclient(t, server.ListenAddr, nil)
token1 := uuid.New()
c := testutil.NewTestHTTPclient(t, server.ListenAddr, &token1)

// Block 1
server.VochainAPP.AdvanceTestBlock()
Expand All @@ -672,6 +673,13 @@ func TestAPINextElectionID(t *testing.T) {
server.VochainAPP.AdvanceTestBlock()
waitUntilHeight(t, c, 2)

// create a new process
censusRoot := createCensus(t, c)

// Block 3
server.VochainAPP.AdvanceTestBlock()
waitUntilHeight(t, c, 3)

// create request for calling api elections/id
body := api.NextElectionID{
OrganizationID: signer.Address().Bytes(),
Expand All @@ -683,19 +691,32 @@ func TestAPINextElectionID(t *testing.T) {
UniqueValues bool `json:"uniqueValues"`
CostFromWeight bool `json:"costFromWeight"`
}{
Serial: true,
Anonymous: true,
EncryptedVotes: true,
UniqueValues: true,
CostFromWeight: true,
Serial: false,
Anonymous: false,
EncryptedVotes: false,
UniqueValues: false,
CostFromWeight: false,
},
}
resp, code := c.Request("POST", body, "elections", "id")
qt.Assert(t, code, qt.Equals, 200, qt.Commentf("response: %s", resp))

nextElectionID := struct {
ElectionID string `json:"electionID"`
ElectionID []byte `json:"electionID"`
}{}
err := json.Unmarshal(resp, &nextElectionID)
qt.Assert(t, err, qt.IsNil)

// create a new election
electionParams := electionprice.ElectionParameters{ElectionDuration: 100, MaxCensusSize: 100}
response := createElection(t, c, signer, electionParams, censusRoot, 0, server.VochainAPP.ChainID())

electionId := response.ElectionID

// Block 4
server.VochainAPP.AdvanceTestBlock()
waitUntilHeight(t, c, 4)

// check next election id is the same as the election id created
qt.Assert(t, hex.EncodeToString(nextElectionID.ElectionID), qt.Equals, electionId.String())
}

0 comments on commit 244ed50

Please sign in to comment.