Skip to content

Commit 545bc3e

Browse files
committed
Add some meaningful backend and frontend tests
1 parent a02104e commit 545bc3e

File tree

2 files changed

+180
-10
lines changed

2 files changed

+180
-10
lines changed

src/frontend/packages/core/src/shared/components/page-header/page-header.component.spec.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
2+
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';
23
import { ActivatedRoute } from '@angular/router';
34
import { RouterTestingModule } from '@angular/router/testing';
45
import { StoreModule } from '@ngrx/store';
@@ -17,7 +18,16 @@ import { PageHeaderModule } from './page-header.module';
1718
describe('PageHeaderComponent', () => {
1819
let component: PageHeaderComponent;
1920
let fixture: ComponentFixture<PageHeaderComponent>;
21+
let httpMock: HttpTestingController;
2022
const URL_KEY = 'key';
23+
const TOKEN_RESPONSE = {
24+
data: {
25+
auth_token: "auth_token_mock_value",
26+
refresh_token: "refresh_token_mock_value",
27+
token_expiry: 1753176322,
28+
}
29+
};
30+
2131
beforeEach(waitForAsync(() => {
2232
TestBed.configureTestingModule({
2333
providers: [
@@ -39,6 +49,7 @@ describe('PageHeaderComponent', () => {
3949
CoreModule,
4050
SharedModule,
4151
PageHeaderModule,
52+
HttpClientTestingModule,
4253
RouterTestingModule,
4354
StoreModule.forRoot(
4455
appReducers
@@ -51,9 +62,14 @@ describe('PageHeaderComponent', () => {
5162
beforeEach(() => {
5263
fixture = TestBed.createComponent(PageHeaderComponent);
5364
component = fixture.componentInstance;
65+
httpMock = TestBed.inject(HttpTestingController);
5466
fixture.detectChanges();
5567
});
5668

69+
afterEach(() => {
70+
httpMock.verify();
71+
});
72+
5773
it('should be created', () => {
5874
expect(component).toBeTruthy();
5975
});
@@ -118,4 +134,43 @@ describe('PageHeaderComponent', () => {
118134
expect(breadcrumbDefinitions).toBeDefined();
119135
expect(breadcrumbDefinitions.length).toEqual(2);
120136
});
137+
138+
it('should have an auth token', (done) => {
139+
component.authToken$.subscribe(data => {
140+
expect(data).toEqual(TOKEN_RESPONSE.data.auth_token);
141+
done();
142+
});
143+
144+
const req = httpMock.expectOne('/api/v1/auth/token');
145+
expect(req.request.method).toBe('GET');
146+
req.flush(TOKEN_RESPONSE);
147+
148+
fixture.detectChanges();
149+
})
150+
151+
it('should have a refresh token', (done) => {
152+
component.refreshToken$.subscribe(data => {
153+
expect(data).toEqual(TOKEN_RESPONSE.data.refresh_token);
154+
done();
155+
});
156+
157+
const req = httpMock.expectOne('/api/v1/auth/token');
158+
expect(req.request.method).toBe('GET');
159+
req.flush(TOKEN_RESPONSE);
160+
161+
fixture.detectChanges();
162+
})
163+
164+
it('should have a token expiry', (done) => {
165+
component.tokenExpiry$.subscribe(data => {
166+
expect(data).toEqual(new Date(TOKEN_RESPONSE.data.token_expiry * 1000));
167+
done();
168+
});
169+
170+
const req = httpMock.expectOne('/api/v1/auth/token');
171+
expect(req.request.method).toBe('GET');
172+
req.flush(TOKEN_RESPONSE);
173+
174+
fixture.detectChanges();
175+
})
121176
});

src/jetstream/session_test.go

Lines changed: 125 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,101 @@ import (
1111
. "github.com/smartystreets/goconvey/convey"
1212
)
1313

14+
/*
15+
Portal Proxy mock that errors out when looking for a session
16+
*/
1417
type sessionTestPortalProxy struct {
1518
portalProxy
1619
}
1720

18-
type testEchoContext struct {
21+
func (*sessionTestPortalProxy) GetSession(_ echo.Context) (*sessions.Session, error) {
22+
return nil, errors.New("Test error")
23+
}
24+
25+
/*
26+
Echo context mock that will make everything work and bypass all session checks
27+
*/
28+
type testEchoContextBypass struct {
1929
echo.Context
30+
JSONResponseVerifier func(statusCode int, input any) error
2031
}
2132

22-
func (s *testEchoContext) Request() *http.Request {
23-
return nil
33+
func (*testEchoContextBypass) Request() *http.Request {
34+
return &http.Request{}
2435
}
2536

26-
func (s *testEchoContext) Get(key string) interface{} {
37+
func (*testEchoContextBypass) Get(_ string) any {
2738
return nil
2839
}
2940

30-
func (p *sessionTestPortalProxy) GetSession(c echo.Context) (*sessions.Session, error) {
31-
return nil, errors.New("Test error")
41+
func (*testEchoContextBypass) Set(_ string, _ any) {}
42+
43+
func (c *testEchoContextBypass) JSON(statusCode int, input any) error {
44+
return c.JSONResponseVerifier(statusCode, input)
3245
}
3346

34-
type testSessionStore struct {
47+
type testSessionStoreFailure struct {
3548
api.SessionStorer
3649
}
3750

38-
func (p *testSessionStore) Get(req *http.Request, name string) (*sessions.Session, error) {
51+
func (p *testSessionStoreFailure) Get(_ *http.Request, _ string) (*sessions.Session, error) {
3952
return nil, errors.New("Test error")
4053
}
54+
55+
type testSessionStoreSuccessful struct {
56+
api.SessionStorer
57+
}
58+
59+
func (p *testSessionStoreSuccessful) Get(_ *http.Request, _ string) (*sessions.Session, error) {
60+
return &sessions.Session{
61+
ID: "00000000-0000-0000-0000-000000000001",
62+
Values: map[any]any{
63+
"exp": int64(9223372036854775807),
64+
"user_id": "mockuid",
65+
},
66+
Options: &sessions.Options{},
67+
IsNew: false,
68+
}, nil
69+
}
70+
71+
type testStratosAuthService struct {
72+
api.StratosAuth
73+
}
74+
75+
func (s *testStratosAuthService) BeforeVerifySession(c echo.Context) {}
76+
func (s *testStratosAuthService) VerifySession(c echo.Context, sessionUser string, expiryTime int64) error {
77+
return nil
78+
}
79+
80+
type testStoreFactory struct {
81+
api.StoreFactory
82+
}
83+
84+
func (s *testStoreFactory) TokenStore() (api.TokenRepository, error) {
85+
return &testTokenStore{}, nil
86+
}
87+
88+
type testTokenStore struct {
89+
api.TokenRepository
90+
}
91+
92+
func (s *testTokenStore) FindAuthToken(userGUID string, encryptionKey []byte) (api.TokenRecord, error) {
93+
return api.TokenRecord{
94+
TokenGUID: "mockTokenGUID",
95+
AuthToken: "mockAuthToken",
96+
RefreshToken: "mockRefreshToken",
97+
TokenExpiry: 1,
98+
Disconnected: false,
99+
AuthType: "mockAuthType",
100+
Metadata: "mockMetadata",
101+
SystemShared: false,
102+
LinkedGUID: "mockLinkedGUID",
103+
Certificate: "mockCertificate",
104+
CertificateKey: "mockCertificateKey",
105+
Enabled: false,
106+
}, nil
107+
}
108+
41109
func TestSession(t *testing.T) {
42110

43111
Convey("Check error struct", t, func() {
@@ -52,10 +120,10 @@ func TestSession(t *testing.T) {
52120
Convey("Check error conditions when no session avaialable", t, func() {
53121

54122
pp := &portalProxy{
55-
SessionStore: &testSessionStore{},
123+
SessionStore: &testSessionStoreFailure{},
56124
}
57125

58-
ctx := &testEchoContext{}
126+
ctx := &testEchoContextBypass{}
59127

60128
_, err := pp.GetSession(ctx)
61129
So(err, ShouldNotBeNil)
@@ -74,4 +142,51 @@ func TestSession(t *testing.T) {
74142
So(err, ShouldNotBeNil)
75143
})
76144

145+
Convey("Return the token on request for the current session when session is valid", t, func() {
146+
pp := &portalProxy{
147+
SessionStore: &testSessionStoreSuccessful{},
148+
StratosAuthService: &testStratosAuthService{},
149+
StoreFactory: &testStoreFactory{},
150+
}
151+
152+
ctx := &testEchoContextBypass{JSONResponseVerifier: func(s int, i any) error {
153+
So(s, ShouldEqual, http.StatusOK)
154+
t, ok := i.(AuthTokenEnvelope)
155+
So(ok, ShouldBeTrue)
156+
So(t.Error, ShouldBeEmpty)
157+
So(t.Status, ShouldEqual, "ok")
158+
So(t.Data.AuthToken, ShouldEqual, "mockAuthToken")
159+
So(t.Data.RefreshToken, ShouldEqual, "mockRefreshToken")
160+
So(t.Data.TokenExpiry, ShouldEqual, 1)
161+
162+
return nil
163+
}}
164+
165+
err := pp.retrieveToken(ctx)
166+
167+
So(err, ShouldBeNil)
168+
})
169+
170+
Convey("Return error message and no token on request if current session is invalid", t, func() {
171+
pp := &portalProxy{
172+
SessionStore: &testSessionStoreFailure{},
173+
StratosAuthService: &testStratosAuthService{},
174+
StoreFactory: &testStoreFactory{},
175+
}
176+
177+
ctx := &testEchoContextBypass{JSONResponseVerifier: func(s int, i any) error {
178+
So(s, ShouldEqual, http.StatusOK)
179+
t, ok := i.(AuthTokenEnvelope)
180+
So(ok, ShouldBeTrue)
181+
So(t.Error, ShouldNotBeBlank)
182+
So(t.Status, ShouldEqual, "error")
183+
So(t.Data, ShouldBeNil)
184+
return nil
185+
}}
186+
187+
err := pp.retrieveToken(ctx)
188+
189+
So(err, ShouldBeNil)
190+
})
191+
77192
}

0 commit comments

Comments
 (0)