summaryrefslogtreecommitdiffstats
path: root/src/pybind/mgr/dashboard/frontend/src/app/ceph/block/iscsi-target-discovery-modal/iscsi-target-discovery-modal.component.spec.ts
blob: 0f540f18e9b4a4c48401f6cb18c167cef0198ab6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import {
  HttpClientTestingModule,
  HttpTestingController,
  TestRequest
} from '@angular/common/http/testing';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { ReactiveFormsModule } from '@angular/forms';
import { By } from '@angular/platform-browser';
import { RouterTestingModule } from '@angular/router/testing';

import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { ToastrModule } from 'ngx-toastr';

import { Permission } from '~/app/shared/models/permissions';
import { SharedModule } from '~/app/shared/shared.module';
import { configureTestBed, FormHelper, IscsiHelper } from '~/testing/unit-test-helper';
import { IscsiTargetDiscoveryModalComponent } from './iscsi-target-discovery-modal.component';

describe('IscsiTargetDiscoveryModalComponent', () => {
  let component: IscsiTargetDiscoveryModalComponent;
  let fixture: ComponentFixture<IscsiTargetDiscoveryModalComponent>;
  let httpTesting: HttpTestingController;
  let req: TestRequest;

  const elem = (css: string) => fixture.debugElement.query(By.css(css));
  const elemDisabled = (css: string) => elem(css).nativeElement.disabled;

  configureTestBed({
    declarations: [IscsiTargetDiscoveryModalComponent],
    imports: [
      HttpClientTestingModule,
      ReactiveFormsModule,
      SharedModule,
      ToastrModule.forRoot(),
      RouterTestingModule
    ],
    providers: [NgbActiveModal]
  });

  beforeEach(() => {
    fixture = TestBed.createComponent(IscsiTargetDiscoveryModalComponent);
    component = fixture.componentInstance;
    httpTesting = TestBed.inject(HttpTestingController);
  });

  describe('with update permissions', () => {
    beforeEach(() => {
      component.permission = new Permission(['update']);
      fixture.detectChanges();
      req = httpTesting.expectOne('api/iscsi/discoveryauth');
    });

    it('should create', () => {
      expect(component).toBeTruthy();
    });

    it('should create form', () => {
      expect(component.discoveryForm.value).toEqual({
        user: '',
        password: '',
        mutual_user: '',
        mutual_password: ''
      });
    });

    it('should patch form', () => {
      req.flush({
        user: 'foo',
        password: 'bar',
        mutual_user: 'mutual_foo',
        mutual_password: 'mutual_bar'
      });
      expect(component.discoveryForm.value).toEqual({
        user: 'foo',
        password: 'bar',
        mutual_user: 'mutual_foo',
        mutual_password: 'mutual_bar'
      });
    });

    it('should submit new values', () => {
      component.discoveryForm.patchValue({
        user: 'new_user',
        password: 'new_pass',
        mutual_user: 'mutual_new_user',
        mutual_password: 'mutual_new_pass'
      });
      component.submitAction();

      const submit_req = httpTesting.expectOne('api/iscsi/discoveryauth');
      expect(submit_req.request.method).toBe('PUT');
      expect(submit_req.request.body).toEqual({
        user: 'new_user',
        password: 'new_pass',
        mutual_user: 'mutual_new_user',
        mutual_password: 'mutual_new_pass'
      });
    });

    it('should enable form if user has update permission', () => {
      expect(elemDisabled('input#user')).toBeFalsy();
      expect(elemDisabled('input#password')).toBeFalsy();
      expect(elemDisabled('input#mutual_user')).toBeFalsy();
      expect(elemDisabled('input#mutual_password')).toBeFalsy();
      expect(elem('cd-submit-button')).toBeDefined();
    });
  });

  it('should disabled form if user does not have update permission', () => {
    component.permission = new Permission(['read', 'create', 'delete']);
    fixture.detectChanges();
    req = httpTesting.expectOne('api/iscsi/discoveryauth');

    expect(elemDisabled('input#user')).toBeTruthy();
    expect(elemDisabled('input#password')).toBeTruthy();
    expect(elemDisabled('input#mutual_user')).toBeTruthy();
    expect(elemDisabled('input#mutual_password')).toBeTruthy();
    expect(elem('cd-submit-button')).toBeNull();
  });

  it('should validate authentication', () => {
    component.permission = new Permission(['read', 'create', 'update', 'delete']);
    fixture.detectChanges();
    const control = component.discoveryForm;
    const formHelper = new FormHelper(control);
    formHelper.expectValid(control);

    IscsiHelper.validateUser(formHelper, 'user');
    IscsiHelper.validatePassword(formHelper, 'password');
    IscsiHelper.validateUser(formHelper, 'mutual_user');
    IscsiHelper.validatePassword(formHelper, 'mutual_password');
  });
});