Skip to content

Commit

Permalink
[Amir] passing through user email in the context to be written to db
Browse files Browse the repository at this point in the history
  • Loading branch information
anagri committed Sep 27, 2018
1 parent fa91802 commit 491def8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions proctord/jobs/execution/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (executioner *executioner) Handle() http.HandlerFunc {

var job Job
err := json.NewDecoder(req.Body).Decode(&job)
userEmail := req.Header.Get(utility.UserEmailHeaderKey)
defer req.Body.Close()
if err != nil {
logger.Error("Error parsing request body", err.Error())
Expand All @@ -77,6 +78,7 @@ func (executioner *executioner) Handle() http.HandlerFunc {
return
}
ctx = context.WithValue(ctx, utility.JobNameContextKey, job.Name)
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail)
ctx = context.WithValue(ctx, utility.JobArgsContextKey, job.Args)

jobMetadata, err := executioner.metadataStore.GetJobMetadata(job.Name)
Expand Down
12 changes: 12 additions & 0 deletions proctord/jobs/execution/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func (suite *ExecutionerTestSuite) TestSuccessfulJobExecution() {
t := suite.T()

jobName := "sample-job-name"
userEmail := "mrproctor@go-jek.com"
jobArgs := map[string]string{
"argOne": "sample-arg",
"argTwo": "another-arg",
Expand All @@ -71,6 +72,7 @@ func (suite *ExecutionerTestSuite) TestSuccessfulJobExecution() {
assert.NoError(t, err)

req := httptest.NewRequest("POST", "/execute", bytes.NewReader(requestBody))
req.Header.Set(utility.UserEmailHeaderKey, userEmail)
responseRecorder := httptest.NewRecorder()

jobMetadata := metadata.Metadata{
Expand All @@ -90,6 +92,7 @@ func (suite *ExecutionerTestSuite) TestSuccessfulJobExecution() {

ctx := context.Background()
ctx = context.WithValue(ctx, utility.JobNameContextKey, jobName)
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail)
ctx = context.WithValue(ctx, utility.JobArgsContextKey, jobArgs)
ctx = context.WithValue(ctx, utility.ImageNameContextKey, jobMetadata.ImageName)
ctx = context.WithValue(ctx, utility.JobNameSubmittedForExecutionContextKey, JobNameSubmittedForExecution)
Expand Down Expand Up @@ -134,19 +137,22 @@ func (suite *ExecutionerTestSuite) TestJobExecutionOnImageLookupFailuer() {
t := suite.T()

jobName := "sample-job-name"
userEmail := "mrproctor@go-jek.com"
job := Job{
Name: jobName,
}
requestBody, err := json.Marshal(job)
assert.NoError(t, err)

req := httptest.NewRequest("POST", "/execute", bytes.NewReader(requestBody))
req.Header.Set(utility.UserEmailHeaderKey, userEmail)
responseRecorder := httptest.NewRecorder()

suite.mockMetadataStore.On("GetJobMetadata", jobName).Return(&metadata.Metadata{}, errors.New("No image found for job name")).Once()

ctx := context.Background()
ctx = context.WithValue(ctx, utility.JobNameContextKey, jobName)
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail)
ctx = context.WithValue(ctx, utility.JobArgsContextKey, job.Args)
ctx = context.WithValue(ctx, utility.JobSubmissionStatusContextKey, utility.JobSubmissionServerError)
suite.mockAuditor.On("AuditJobsExecution", ctx).Return().Once()
Expand All @@ -166,13 +172,15 @@ func (suite *ExecutionerTestSuite) TestJobExecutionOnSecretsFetchFailuer() {
t := suite.T()

jobName := "sample-job-name"
userEmail := "mrproctor@go-jek.com"
job := Job{
Name: jobName,
}
requestBody, err := json.Marshal(job)
assert.NoError(t, err)

req := httptest.NewRequest("POST", "/execute", bytes.NewReader(requestBody))
req.Header.Set(utility.UserEmailHeaderKey, userEmail)
responseRecorder := httptest.NewRecorder()

suite.mockMetadataStore.On("GetJobMetadata", jobName).Return(&metadata.Metadata{}, nil).Once()
Expand All @@ -182,6 +190,7 @@ func (suite *ExecutionerTestSuite) TestJobExecutionOnSecretsFetchFailuer() {

ctx := context.Background()
ctx = context.WithValue(ctx, utility.JobNameContextKey, jobName)
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail)
ctx = context.WithValue(ctx, utility.JobArgsContextKey, job.Args)
ctx = context.WithValue(ctx, utility.ImageNameContextKey, "")
ctx = context.WithValue(ctx, utility.JobSubmissionStatusContextKey, utility.JobSubmissionServerError)
Expand All @@ -202,6 +211,7 @@ func (suite *ExecutionerTestSuite) TestJobExecutionOnExecutionFailure() {
t := suite.T()

jobName := "sample-job-name"
userEmail := "mrproctor@go-jek.com"
emptyMap := make(map[string]string)
job := Job{
Name: jobName,
Expand All @@ -212,6 +222,7 @@ func (suite *ExecutionerTestSuite) TestJobExecutionOnExecutionFailure() {
assert.NoError(t, err)

req := httptest.NewRequest("POST", "/execute", bytes.NewReader(requestBody))
req.Header.Set(utility.UserEmailHeaderKey, userEmail)
responseRecorder := httptest.NewRecorder()

jobMetadata := metadata.Metadata{
Expand All @@ -225,6 +236,7 @@ func (suite *ExecutionerTestSuite) TestJobExecutionOnExecutionFailure() {

ctx := context.Background()
ctx = context.WithValue(ctx, utility.JobNameContextKey, job.Name)
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail)
ctx = context.WithValue(ctx, utility.JobArgsContextKey, job.Args)
ctx = context.WithValue(ctx, utility.ImageNameContextKey, jobMetadata.ImageName)
ctx = context.WithValue(ctx, utility.JobSubmissionStatusContextKey, utility.JobSubmissionServerError)
Expand Down
2 changes: 2 additions & 0 deletions proctord/utility/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package utility

const UserEmailHeaderKey = "Email-Id"

const ClientError = "malformed request"
const ServerError = "Something went wrong"

Expand Down

0 comments on commit 491def8

Please sign in to comment.