Skip to content

Commit

Permalink
record event when execute actions emails is called
Browse files Browse the repository at this point in the history
  • Loading branch information
bsoniam committed Dec 20, 2019
1 parent d428668 commit a71aba8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
11 changes: 10 additions & 1 deletion pkg/management/component.go
Expand Up @@ -589,15 +589,24 @@ func (c *component) ExecuteActionsEmail(ctx context.Context, realmName string, u
var accessToken = ctx.Value(cs.CtContextAccessToken).(string)

var actions = []string{}

var eventRecorded bool = false
for _, requiredAction := range requiredActions {
actions = append(actions, string(requiredAction))
if string(requiredAction) == initPasswordAction {
//store the API call into the DB
c.reportEvent(ctx, "INIT_PASSWORD", database.CtEventRealmName, realmName, database.CtEventUserID, userID)
eventRecorded = true
}
}

if !eventRecorded {
//store the API call into the DB with the parameters and the required actions
listActions := strings.Join(actions, ",")
values := append(paramKV, "required_actions", listActions)
additionalInfo := database.CreateAdditionalInfo(values...)
c.reportEvent(ctx, "ACTION_EMAIL", database.CtEventRealmName, realmName, database.CtEventUserID, userID, database.CtEventAdditionalInfo, additionalInfo)
}

err := c.keycloakClient.ExecuteActionsEmail(accessToken, realmName, userID, actions, paramKV...)

if err != nil {
Expand Down
16 changes: 14 additions & 2 deletions pkg/management/component_test.go
Expand Up @@ -1567,7 +1567,8 @@ func TestExecuteActionsEmail(t *testing.T) {
var userID = "1245-7854-8963"
var reqActions = []api.RequiredAction{initPasswordAction, "action1", "action2"}
var actions = []string{initPasswordAction, "action1", "action2"}

var actions2 = []string{"action1", "action2"}
var reqActions2 = []api.RequiredAction{"action1", "action2"}
var key1 = "key1"
var value1 = "value1"
var key2 = "key2"
Expand All @@ -1585,7 +1586,6 @@ func TestExecuteActionsEmail(t *testing.T) {

assert.Nil(t, err)
}

// Error
{
mockKeycloakClient.EXPECT().ExecuteActionsEmail(accessToken, realmName, userID, actions).Return(fmt.Errorf("Invalid input")).Times(1)
Expand All @@ -1596,6 +1596,18 @@ func TestExecuteActionsEmail(t *testing.T) {

assert.NotNil(t, err)
}
// Send email actions, but not sms-password-set
{

mockKeycloakClient.EXPECT().ExecuteActionsEmail(accessToken, realmName, userID, actions2, key1, value1, key2, value2).Return(nil).Times(1)

var ctx = context.WithValue(context.Background(), cs.CtContextAccessToken, accessToken)
mockEventDBModule.EXPECT().ReportEvent(ctx, "ACTION_EMAIL", "back-office", gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil).Times(1)

err := managementComponent.ExecuteActionsEmail(ctx, "master", userID, reqActions2, key1, value1, key2, value2)

assert.Nil(t, err)
}
}

func TestSendNewEnrolmentCode(t *testing.T) {
Expand Down

0 comments on commit a71aba8

Please sign in to comment.