Skip to content

Commit

Permalink
Programming exercises: Fix code button showing HTTPS link when it sho…
Browse files Browse the repository at this point in the history
…uld be disabled (#9696)
  • Loading branch information
SimonEntholzer authored Nov 10, 2024
1 parent cec3866 commit afa8543
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,19 @@ export class CodeButtonComponent implements OnInit, OnChanges {
private ideSettingsService: IdeSettingsService,
) {}

ngOnInit() {
this.accountService
.identity()
.then((user) => {
this.user = user!;
this.refreshTokenState();

this.copyEnabled = true;
this.useSsh = this.localStorage.retrieve('useSsh') || false;
this.useToken = this.localStorage.retrieve('useToken') || false;
this.localStorage.observe('useSsh').subscribe((useSsh) => (this.useSsh = useSsh || false));
this.localStorage.observe('useToken').subscribe((useToken) => (this.useToken = useToken || false));

if (this.useSsh) {
this.useSshUrl();
}
if (this.useToken) {
this.useHttpsUrlWithToken();
}
})
.then(() => this.loadParticipationVcsAccessTokens());
async ngOnInit() {
const user = await this.accountService.identity();
if (!user) {
return;
}
this.user = user;

this.refreshTokenState();

this.copyEnabled = true;
this.useSsh = this.localStorage.retrieve('useSsh') || false;
this.useToken = this.localStorage.retrieve('useToken') || false;
this.loadParticipationVcsAccessTokens();

// Get ssh information from the user
this.profileService.getProfileInfo().subscribe((profileInfo) => {
Expand All @@ -124,6 +116,13 @@ export class CodeButtonComponent implements OnInit, OnChanges {
this.sshSettingsUrl = profileInfo.sshKeysURL;
}
this.sshKeyMissingTip = this.formatTip('artemisApp.exerciseActions.sshKeyTip', this.sshSettingsUrl);

if (this.useSsh) {
this.useSshUrl();
}
if (this.useToken) {
this.useHttpsUrlWithToken();
}
});

this.ideSettingsService.loadIdePreferences().then((programmingLanguageToIde) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { SafeUrlPipe } from 'app/shared/pipes/safe-url.pipe';
import dayjs from 'dayjs/esm';
import { MockComponent, MockDirective, MockPipe, MockProvider } from 'ng-mocks';
import { LocalStorageService } from 'ngx-webstorage';
import { BehaviorSubject, Subject, of, throwError } from 'rxjs';
import { BehaviorSubject, of, throwError } from 'rxjs';
import { MockAccountService } from '../../helpers/mocks/service/mock-account.service';
import { MockFeatureToggleService } from '../../helpers/mocks/service/mock-feature-toggle.service';
import { MockProfileService } from '../../helpers/mocks/service/mock-profile.service';
Expand All @@ -36,9 +36,6 @@ describe('CodeButtonComponent', () => {
let profileService: ProfileService;
let accountService: AccountService;

let localStorageUseSshRetrieveStub: jest.SpyInstance;
let localStorageUseSshObserveStub: jest.SpyInstance;
let localStorageUseSshObserveStubSubject: Subject<boolean | undefined>;
let localStorageUseSshStoreStub: jest.SpyInstance;
let getVcsAccessTokenSpy: jest.SpyInstance;
let createVcsAccessTokenSpy: jest.SpyInstance;
Expand Down Expand Up @@ -118,15 +115,10 @@ describe('CodeButtonComponent', () => {
accountService = TestBed.inject(AccountService);

const localStorageMock = fixture.debugElement.injector.get(LocalStorageService);
localStorageUseSshRetrieveStub = jest.spyOn(localStorageMock, 'retrieve');
localStorageUseSshObserveStub = jest.spyOn(localStorageMock, 'observe');
localStorageUseSshStoreStub = jest.spyOn(localStorageMock, 'store');
getVcsAccessTokenSpy = jest.spyOn(accountService, 'getVcsAccessToken');
createVcsAccessTokenSpy = jest.spyOn(accountService, 'createVcsAccessToken');

localStorageUseSshObserveStubSubject = new Subject();
localStorageUseSshObserveStub.mockReturnValue(localStorageUseSshObserveStubSubject);

participation = {};
component.user = user;
});
Expand All @@ -145,50 +137,46 @@ describe('CodeButtonComponent', () => {
jest.restoreAllMocks();
});

it('should initialize', fakeAsync(() => {
it('should initialize', async () => {
stubServices();

component.ngOnInit();
tick();
await component.ngOnInit();

expect(component.sshSettingsUrl).toBe(`${window.location.origin}/user-settings/ssh`);
expect(component.sshTemplateUrl).toBe(info.sshCloneURLTemplate);
expect(component.sshEnabled).toBe(!!info.sshCloneURLTemplate);
expect(component.versionControlUrl).toBe(info.versionControlUrl);
}));
});

it('should create new vcsAccessToken when it does not exist', fakeAsync(() => {
it('should create new vcsAccessToken when it does not exist', async () => {
createVcsAccessTokenSpy = jest.spyOn(accountService, 'createVcsAccessToken').mockReturnValue(of(new HttpResponse({ body: vcsToken })));
getVcsAccessTokenSpy = jest.spyOn(accountService, 'getVcsAccessToken').mockReturnValue(throwError(() => new HttpErrorResponse({ status: 404, statusText: 'Not found' })));
stubServices();
participation.id = 1;
component.useParticipationVcsAccessToken = true;
component.participations = [participation];
await component.ngOnInit();
component.ngOnChanges();
tick();
component.ngOnInit();
tick();

expect(component.accessTokensEnabled).toBeTrue();
expect(component.user.vcsAccessToken).toEqual(vcsToken);
expect(getVcsAccessTokenSpy).toHaveBeenCalled();
expect(createVcsAccessTokenSpy).toHaveBeenCalled();
}));
});

it('should not create new vcsAccessToken when it exists', fakeAsync(() => {
it('should not create new vcsAccessToken when it exists', async () => {
participation.id = 1;
component.participations = [participation];
component.useParticipationVcsAccessToken = true;
stubServices();
await component.ngOnInit();
component.ngOnChanges();
tick();
component.ngOnInit();
tick();

expect(component.accessTokensEnabled).toBeTrue();
expect(component.user.vcsAccessToken).toEqual(vcsToken);
expect(getVcsAccessTokenSpy).toHaveBeenCalled();
expect(createVcsAccessTokenSpy).not.toHaveBeenCalled();
}));
});

it('should get ssh url (same url for team and individual participation)', () => {
participation.repositoryUri = 'https://gitlab.ase.in.tum.de/scm/ITCPLEASE1/itcplease1-exercise.git';
Expand Down Expand Up @@ -365,12 +353,11 @@ describe('CodeButtonComponent', () => {
component.participations = [participation];
component.activeParticipation = participation;
component.sshEnabled = true;
component.accessTokensEnabled = true;

fixture.detectChanges();
tick();

expect(localStorageUseSshRetrieveStub).toHaveBeenNthCalledWith(1, 'useSsh');
expect(localStorageUseSshObserveStub).toHaveBeenNthCalledWith(1, 'useSsh');
expect(component.useSsh).toBeFalse();

fixture.debugElement.query(By.css('.code-button')).nativeElement.click();
Expand All @@ -390,14 +377,6 @@ describe('CodeButtonComponent', () => {
expect(localStorageUseSshStoreStub).toHaveBeenCalledWith('useSsh', false);
expect(component.useSsh).toBeFalse();
expect(component.useToken).toBeTrue();

localStorageUseSshObserveStubSubject.next(true);
tick();
expect(component.useSsh).toBeTrue();

localStorageUseSshObserveStubSubject.next(false);
tick();
expect(component.useSsh).toBeFalse();
}));

it.each([
Expand Down

0 comments on commit afa8543

Please sign in to comment.