From f8965ecab65c77ec62f5ec7ce8a39a9083f605f6 Mon Sep 17 00:00:00 2001 From: Robert Bittle Date: Mon, 5 Nov 2018 09:27:51 -0500 Subject: [PATCH] Allow import/export of session --- authentication.go | 14 +++++++++++++- authentication_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 1 deletion(-) diff --git a/authentication.go b/authentication.go index bd123b90..4114a72b 100644 --- a/authentication.go +++ b/authentication.go @@ -45,7 +45,19 @@ type Session struct { LastFailedLoginTime string `json:"lastFailedLoginTime"` PreviousLoginTime string `json:"previousLoginTime"` } `json:"loginInfo"` - Cookies []*http.Cookie + Cookies []*http.Cookie `json:"cookies"` +} + +// ExportSession exports the current session so it can be reused later +func (s *AuthenticationService) ExportSession() []byte { + session, _ := json.Marshal(s.client.session) + return session +} + +// ImportSession imports a session +func (s *AuthenticationService) ImportSession(session []byte) { + _ = json.Unmarshal(session, &s.client.session) + s.authType = authTypeSession } // AcquireSessionCookieWithContext creates a new session for a user in Jira. diff --git a/authentication_test.go b/authentication_test.go index 431a7fdc..eecf523f 100644 --- a/authentication_test.go +++ b/authentication_test.go @@ -80,6 +80,50 @@ func TestAuthenticationService_AcquireSessionCookie_Success(t *testing.T) { } } +func TestAuthenticationService_AcquireSessionCookieExport(t *testing.T) { + setup() + defer teardown() + testMux.HandleFunc("/rest/auth/1/session", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testRequestURL(t, r, "/rest/auth/1/session") + b, err := ioutil.ReadAll(r.Body) + if err != nil { + t.Errorf("Error in read body: %s", err) + } + if bytes.Index(b, []byte(`"username":"foo"`)) < 0 { + t.Error("No username found") + } + if bytes.Index(b, []byte(`"password":"bar"`)) < 0 { + t.Error("No password found") + } + + fmt.Fprint(w, `{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"}}`) + }) + + _, err := testClient.Authentication.AcquireSessionCookie("foo", "bar") + if err != nil { + t.Errorf("No error expected. Got %s", err) + } + + sessionExport := testClient.Authentication.ExportSession() + expectedSessionExport := `{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"},"cookies":[]}` + if string(sessionExport) != expectedSessionExport { + t.Errorf("Expected sessionExport %s. Got %s", expectedSessionExport, string(sessionExport)) + } +} + +func TestAuthenticationService_SessionImport(t *testing.T) { + setup() + defer teardown() + + session := []byte(`{"session":{"name":"JSESSIONID","value":"12345678901234567890"},"loginInfo":{"failedLoginCount":10,"loginCount":127,"lastFailedLoginTime":"2016-03-16T04:22:35.386+0000","previousLoginTime":"2016-03-16T04:22:35.386+0000"},"cookies":[]}`) + testClient.Authentication.ImportSession(session) + + if testClient.Authentication.authType != authTypeSession { + t.Errorf("Expected authType %d. Got %d", authTypeSession, testClient.Authentication.authType) + } +} + func TestAuthenticationService_SetBasicAuth(t *testing.T) { setup() defer teardown()