Skip to content

Commit

Permalink
[FABG-959] Unable to specify empty affiliation (#70)
Browse files Browse the repository at this point in the history
Fixed a bug which required optional affiliation to be
provided when creating/modifying identities.

Signed-off-by: Aleksandar Likic <[email protected]>
  • Loading branch information
alikic authored Apr 15, 2020
1 parent 60484dd commit 594a8dc
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 30 deletions.
10 changes: 5 additions & 5 deletions pkg/client/msp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,10 +352,10 @@ func TestCreateIdentityFailure(t *testing.T) {
t.Fatalf("failed to create CA client: %s", err)
}

// Missing required affiliation
_, err = c.CreateIdentity(&IdentityRequest{ID: "123"})
if err == nil || !strings.Contains(err.Error(), "ID and affiliation are required") {
t.Fatalf("Should have failed to create identity due to missing affiliation: %s", err)
// Missing required ID
_, err = c.CreateIdentity(&IdentityRequest{})
if err == nil || !strings.Contains(err.Error(), "ID is required") {
t.Fatalf("Should have failed to create identity due to missing ID: %s", err)
}

}
Expand All @@ -371,7 +371,7 @@ func TestModifyIdentityFailure(t *testing.T) {

// Missing required ID
_, err = c.ModifyIdentity(&IdentityRequest{Affiliation: "org2", Secret: "top-secret", Attributes: []Attribute{{Name: "attName1", Value: "attValue1"}}})
if err == nil || !strings.Contains(err.Error(), "ID and affiliation are required") {
if err == nil || !strings.Contains(err.Error(), "ID is required") {
t.Fatalf("Should have failed to update identity due to missing id: %s", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/msp/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ type IdentityRequest struct {
// The enrollment ID which uniquely identifies an identity (required)
ID string

// The identity's affiliation (required)
// The identity's affiliation
Affiliation string

// Array of attributes to assign to the user
Expand Down
24 changes: 12 additions & 12 deletions pkg/msp/caclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ func (c *CAClientImpl) CreateIdentity(request *api.IdentityRequest) (*api.Identi
return nil, errors.New("must provide identity request")
}

// Checke required parameters (ID and affiliation)
if request.ID == "" || request.Affiliation == "" {
return nil, errors.New("ID and affiliation are required")
// Check required parameters (ID)
if request.ID == "" {
return nil, errors.New("ID is required")
}

registrar, err := c.getRegistrar(c.registrar.EnrollID, c.registrar.EnrollSecret)
Expand All @@ -197,9 +197,9 @@ func (c *CAClientImpl) ModifyIdentity(request *api.IdentityRequest) (*api.Identi
return nil, errors.New("must provide identity request")
}

// Checke required parameters (ID and affiliation)
if request.ID == "" || request.Affiliation == "" {
return nil, errors.New("ID and affiliation are required")
// Check required parameters (ID)
if request.ID == "" {
return nil, errors.New("ID is required")
}

registrar, err := c.getRegistrar(c.registrar.EnrollID, c.registrar.EnrollSecret)
Expand All @@ -226,7 +226,7 @@ func (c *CAClientImpl) RemoveIdentity(request *api.RemoveIdentityRequest) (*api.
return nil, errors.New("must provide remove identity request")
}

// Checke required parameters (ID)
// Check required parameters (ID)
if request.ID == "" {
return nil, errors.New("ID is required")
}
Expand All @@ -252,7 +252,7 @@ func (c *CAClientImpl) GetIdentity(id, caname string) (*api.IdentityResponse, er
return nil, fmt.Errorf("no CAs configured for organization: %s", c.orgName)
}

// Checke required parameters (ID and affiliation)
// Check required parameters (ID and affiliation)
if id == "" {
return nil, errors.New("id is required")
}
Expand Down Expand Up @@ -389,7 +389,7 @@ func (c *CAClientImpl) GetAffiliation(affiliation, caname string) (*api.Affiliat
return nil, fmt.Errorf("no CAs configured for organization: %s", c.orgName)
}

// Checke required parameters (affiliation)
// Check required parameters (affiliation)
if affiliation == "" {
return nil, errors.New("affiliation is required")
}
Expand Down Expand Up @@ -426,7 +426,7 @@ func (c *CAClientImpl) AddAffiliation(request *api.AffiliationRequest) (*api.Aff
return nil, errors.New("must provide affiliation request")
}

// Checke required parameters (Name)
// Check required parameters (Name)
if request.Name == "" {
return nil, errors.New("Name is required")
}
Expand All @@ -449,7 +449,7 @@ func (c *CAClientImpl) ModifyAffiliation(request *api.ModifyAffiliationRequest)
return nil, errors.New("must provide affiliation request")
}

// Checke required parameters (Name and NewName)
// Check required parameters (Name and NewName)
if request.Name == "" || request.NewName == "" {
return nil, errors.New("Name and NewName are required")
}
Expand All @@ -472,7 +472,7 @@ func (c *CAClientImpl) RemoveAffiliation(request *api.AffiliationRequest) (*api.
return nil, errors.New("must provide remove affiliation request")
}

// Checke required parameters (Name)
// Check required parameters (Name)
if request.Name == "" {
return nil, errors.New("Name is required")
}
Expand Down
32 changes: 20 additions & 12 deletions pkg/msp/caclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,12 +221,7 @@ func TestCreateIdentity(t *testing.T) {

// Create without required parameters
_, err = f.caClient.CreateIdentity(&api.IdentityRequest{Affiliation: "Org1"})
if err == nil || !strings.Contains(err.Error(), "ID and affiliation are required") {
t.Fatal("Expected error due to missing required parameters")
}

_, err = f.caClient.CreateIdentity(&api.IdentityRequest{ID: "Some name"})
if err == nil || !strings.Contains(err.Error(), "ID and affiliation are required") {
if err == nil || !strings.Contains(err.Error(), "ID is required") {
t.Fatal("Expected error due to missing required parameters")
}

Expand All @@ -241,6 +236,15 @@ func TestCreateIdentity(t *testing.T) {
if identity.Secret != "top-secret" {
t.Fatalf("create identity returned wrong value %s", identity.Secret)
}

// Create identity with ID only
identity, err = f.caClient.CreateIdentity(&api.IdentityRequest{ID: "test1"})
if err != nil {
t.Fatalf("create identity return error %s", err)
}
if identity.Secret != "top-secret" {
t.Fatalf("create identity returned wrong value %s", identity.Secret)
}
}

// TestModifyIdentity tests updating identity
Expand All @@ -258,12 +262,7 @@ func TestModifyIdentity(t *testing.T) {

// Update without required parameters
_, err = f.caClient.ModifyIdentity(&api.IdentityRequest{Affiliation: "Org1"})
if err == nil || !strings.Contains(err.Error(), "ID and affiliation are required") {
t.Fatal("Expected error due to missing required parameters")
}

_, err = f.caClient.ModifyIdentity(&api.IdentityRequest{ID: "Some name"})
if err == nil || !strings.Contains(err.Error(), "ID and affiliation are required") {
if err == nil || !strings.Contains(err.Error(), "ID is required") {
t.Fatal("Expected error due to missing required parameters")
}

Expand All @@ -275,6 +274,15 @@ func TestModifyIdentity(t *testing.T) {
if identity.Secret != "new-top-secret" {
t.Fatalf("update identity returned wrong value: %s", identity.Secret)
}

// Update identity without affiliation
identity, err = f.caClient.ModifyIdentity(&api.IdentityRequest{ID: "123", Secret: "new-top-secret"})
if err != nil {
t.Fatalf("update identity return error %s", err)
}
if identity.Secret != "new-top-secret" {
t.Fatalf("update identity returned wrong value: %s", identity.Secret)
}
}

// TestRemoveIdentity tests removing an identity
Expand Down

0 comments on commit 594a8dc

Please sign in to comment.