Skip to content

Commit

Permalink
Merge pull request #170 from MaximeZmt/main
Browse files Browse the repository at this point in the history
End Of Semester Project - Merge
  • Loading branch information
PascalinDe authored Jul 2, 2024
2 parents f95d249 + 34c8feb commit 2793657
Show file tree
Hide file tree
Showing 38 changed files with 2,542 additions and 361 deletions.
76 changes: 76 additions & 0 deletions contracts/evoting/admin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package evoting

import (
"github.com/c4dt/d-voting/contracts/evoting/types"
"github.com/stretchr/testify/require"
"go.dedis.ch/dela/serde"
sjson "go.dedis.ch/dela/serde/json"
"testing"
)

var ctxAdminTest serde.Context

var formFacAdminTest serde.Factory
var transactionFacAdminTest serde.Factory

func init() {
ciphervoteFac := types.CiphervoteFactory{}
formFacAdminTest = types.NewFormFactory(ciphervoteFac, fakeAuthorityFactory{})
transactionFacAdminTest = types.NewTransactionFactory(ciphervoteFac)

ctxAdminTest = sjson.NewContext()
}

// This test create an Admin Form structure which is then serialized and
// deserialized to check whether these operations work as intended.
// Serialization/Deserialization of an AdminList should not change its values.
func TestAdmin_Serde(t *testing.T) {
initialAdminList := []int{111111, 222222, 333333, 123456}

adminList := types.AdminList{AdminList: initialAdminList}

value, err := adminList.Serialize(ctxAdminTest)

require.NoError(t, err)

// deserialization
deserializedAdminList := types.AdminList{}

msgs, err := deserializedAdminList.Deserialize(ctxAdminTest, value)

require.NoError(t, err)

updatedAdminList := msgs.(types.AdminList)

require.Equal(t, initialAdminList, updatedAdminList.AdminList)
}

func TestAdmin_AddAdminAndRemoveAdmin(t *testing.T) {
initialAdminList := []int{}

myTestID := "123456"

adminList := types.AdminList{AdminList: initialAdminList}

res, err := adminList.GetAdminIndex(myTestID)
require.Equal(t, -1, res)
require.NoError(t, err)

err = adminList.AddAdmin(myTestID)
require.NoError(t, err)
res, err = adminList.GetAdminIndex(myTestID)
require.Equal(t, 0, res)
require.NoError(t, err)

err = adminList.RemoveAdmin(myTestID)
require.ErrorContains(t, err, "cannot remove this Admin because it is the only one remaining")

err = adminList.AddAdmin("654321")
require.NoError(t, err)

err = adminList.RemoveAdmin(myTestID)
require.NoError(t, err)
res, err = adminList.GetAdminIndex(myTestID)
require.Equal(t, -1, res)
require.NoError(t, err)
}
33 changes: 22 additions & 11 deletions contracts/evoting/controller/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ const (
contentType = "application/json"
formPath = "/evoting/forms"
// FormPathSlash is the path to the form with a trailing slash
FormPathSlash = formPath + "/"
formIDPath = FormPathSlash + "{formID}"
transactionSlash = "/evoting/transactions/"
FormPathSlash = formPath + "/"
formIDPath = FormPathSlash + "{formID}"
transactionSlash = "/evoting/transactions/"

evotingPathSlash = "/evoting/"

transactionPath = transactionSlash + "{token}"
unexpectedStatus = "unexpected status: %s, body: %s"
failRetrieveDecryption = "failed to retrieve decryption key: %v"
Expand Down Expand Up @@ -169,12 +172,19 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
return xerrors.Errorf("failed to unmarshal proxy key: %v", err)
}

transactionManager := txnmanager.NewTransactionManager(mngr, p, sjson.NewContext(), proxykey, blocks, signer)
transactionManager := txnmanager.NewTransactionManager(mngr, p, sjson.NewContext(), proxykey, blocks, signer, validation)

ep := eproxy.NewForm(ordering, p, sjson.NewContext(), formFac, proxykey, transactionManager)

router := mux.NewRouter()

router.HandleFunc(evotingPathSlash+"addadmin", ep.AddAdmin).Methods("POST")
router.HandleFunc(evotingPathSlash+"removeadmin", ep.RemoveAdmin).Methods("POST")
router.HandleFunc(evotingPathSlash+"adminlist", ep.AdminList).Methods("GET")
router.HandleFunc(formIDPath+"/addowner", ep.AddOwnerToForm).Methods("POST")
router.HandleFunc(formIDPath+"/removeowner", ep.RemoveOwnerToForm).Methods("POST")
router.HandleFunc(formIDPath+"/addvoter", ep.AddVoterToForm).Methods("POST")
router.HandleFunc(formIDPath+"/removevoter", ep.RemoveVoterToForm).Methods("POST")
router.HandleFunc(formPath, ep.NewForm).Methods("POST")
router.HandleFunc(formPath, ep.Forms).Methods("GET")
router.HandleFunc(formPath, eproxy.AllowCORS).Methods("OPTIONS")
Expand All @@ -188,6 +198,7 @@ func (a *RegisterAction) Execute(ctx node.Context) error {
router.NotFoundHandler = http.HandlerFunc(eproxy.NotFoundHandler)
router.MethodNotAllowedHandler = http.HandlerFunc(eproxy.NotAllowedHandler)

proxy.RegisterHandler(evotingPathSlash, router.ServeHTTP)
proxy.RegisterHandler(formPath, router.ServeHTTP)
proxy.RegisterHandler(FormPathSlash, router.ServeHTTP)
proxy.RegisterHandler(transactionSlash, router.ServeHTTP)
Expand Down Expand Up @@ -352,8 +363,8 @@ func (a *scenarioTestAction) Execute(ctx node.Context) error {
}

castVoteRequest := ptypes.CastVoteRequest{
UserID: "user1",
Ballot: ballot1,
VoterID: "user1",
Ballot: ballot1,
}

signed, err := createSignedRequest(secret, castVoteRequest)
Expand All @@ -377,8 +388,8 @@ func (a *scenarioTestAction) Execute(ctx node.Context) error {
}

castVoteRequest = ptypes.CastVoteRequest{
UserID: "user2",
Ballot: ballot2,
VoterID: "user2",
Ballot: ballot2,
}

signed, err = createSignedRequest(secret, castVoteRequest)
Expand All @@ -402,8 +413,8 @@ func (a *scenarioTestAction) Execute(ctx node.Context) error {
}

castVoteRequest = ptypes.CastVoteRequest{
UserID: "user3",
Ballot: ballot3,
VoterID: "user3",
Ballot: ballot3,
}

signed, err = createSignedRequest(secret, castVoteRequest)
Expand Down Expand Up @@ -593,7 +604,7 @@ func setupSimpleForm(ctx node.Context, secret kyber.Scalar, proxyAddr1 string,

createSimpleFormRequest := ptypes.CreateFormRequest{
Configuration: configuration,
AdminID: "adminId",
UserID: "UserID",
}

signed, err := createSignedRequest(secret, createSimpleFormRequest)
Expand Down
Loading

0 comments on commit 2793657

Please sign in to comment.