forked from dedis/d-voting
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #170 from MaximeZmt/main
End Of Semester Project - Merge
- Loading branch information
Showing
38 changed files
with
2,542 additions
and
361 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.