Skip to content

Commit

Permalink
feat: Exam location (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
ditsuke committed May 12, 2023
1 parent c32f30e commit 387809f
Show file tree
Hide file tree
Showing 16 changed files with 532 additions and 260 deletions.
2 changes: 1 addition & 1 deletion .idea/go-amizone.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 31 additions & 11 deletions amizone/amizone.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ import (
"text/template"
"time"

"k8s.io/klog/v2"

"github.com/ditsuke/go-amizone/amizone/internal"
"github.com/ditsuke/go-amizone/amizone/internal/marshaller"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/internal/validator"
"github.com/ditsuke/go-amizone/amizone/models"
"k8s.io/klog/v2"
)

// Endpoints
const (
BaseUrl = "https://" + internal.AmizoneDomain
BaseURL = "https://" + internal.AmizoneDomain

loginRequestEndpoint = "/"
attendancePageEndpoint = "/Home"
Expand Down Expand Up @@ -159,7 +160,12 @@ func (a *Client) login() error {
return
}()

loginResponse, err := a.doRequest(false, http.MethodPost, loginRequestEndpoint, strings.NewReader(loginRequestData.Encode()))
loginResponse, err := a.doRequest(
false,
http.MethodPost,
loginRequestEndpoint,
strings.NewReader(loginRequestData.Encode()),
)
if err != nil {
klog.Warningf("error while making HTTP request to the amizone login page: %s", err.Error())
return fmt.Errorf("%s: %w", ErrFailedLogin, err)
Expand All @@ -172,14 +178,18 @@ func (a *Client) login() error {
}

if loggedIn := parse.IsLoggedIn(loginResponse.Body); !loggedIn {
klog.Error("login attempt failed as indicated by parsing the page returned after the login request, while the redirect indicated that it passed." +
" this failure indicates that something broke between Amizone and go-amizone.")
klog.Error(
"login attempt failed as indicated by parsing the page returned after the login request, while the redirect indicated that it passed." +
" this failure indicates that something broke between Amizone and go-amizone.",
)
return errors.New(ErrFailedLogin)
}

if !internal.IsLoggedIn(a.httpClient) {
klog.Error("login attempt failed as indicated by checking the cookies in the http client's cookie jar. this failure indicates that something has broken between" +
" Amizone and go-amizone, possibly the cookies used by amizone for authentication.")
klog.Error(
"login attempt failed as indicated by checking the cookies in the http client's cookie jar. this failure indicates that something has broken between" +
" Amizone and go-amizone, possibly the cookies used by amizone for authentication.",
)
return errors.New(ErrFailedLogin)
}

Expand Down Expand Up @@ -212,7 +222,11 @@ func (a *Client) GetClassSchedule(year int, month time.Month, date int) (models.
timeFrom := time.Date(year, month, date, 0, 0, 0, 0, time.UTC)
timeTo := timeFrom.Add(time.Hour * 24)

endpoint := fmt.Sprintf(scheduleEndpointTemplate, timeFrom.Format(classScheduleEndpointDateFormat), timeTo.Format(classScheduleEndpointDateFormat))
endpoint := fmt.Sprintf(
scheduleEndpointTemplate,
timeFrom.Format(classScheduleEndpointDateFormat),
timeTo.Format(classScheduleEndpointDateFormat),
)

response, err := a.doRequest(true, http.MethodGet, endpoint, nil)
if err != nil {
Expand Down Expand Up @@ -418,7 +432,12 @@ func (a *Client) RemoveWifiMac(addr net.HardwareAddr) error {
}

// ! VULN: remove mac addresses registered by anyone if you know the mac/username pair.
response, err := a.doRequest(true, http.MethodGet, fmt.Sprintf(removeWifiMacEndpoint, a.credentials.Username, marshaller.Mac(addr)), nil)
response, err := a.doRequest(
true,
http.MethodGet,
fmt.Sprintf(removeWifiMacEndpoint, a.credentials.Username, marshaller.Mac(addr)),
nil,
)
if err != nil {
klog.Errorf("request (remove wifi mac): %s", err.Error())
return fmt.Errorf("%s: %s", ErrFailedToFetchPage, err.Error())
Expand Down Expand Up @@ -455,9 +474,10 @@ func (a *Client) SubmitFacultyFeedbackHack(rating int32, queryRating int32, comm
}

// Transform queryRating for "higher number is higher rating" semantics (it's the opposite in the form 😭)
if queryRating == 1 {
switch queryRating {
case 1:
queryRating = 3
} else if queryRating == 3 {
case 3:
queryRating = 1
}

Expand Down
7 changes: 4 additions & 3 deletions amizone/amizone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import (
"testing"
"time"

. "github.com/onsi/gomega"
"gopkg.in/h2non/gock.v1"

"github.com/ditsuke/go-amizone/amizone"
"github.com/ditsuke/go-amizone/amizone/internal/mock"
"github.com/ditsuke/go-amizone/amizone/internal/parse"
"github.com/ditsuke/go-amizone/amizone/models"
. "github.com/onsi/gomega"
"gopkg.in/h2non/gock.v1"
)

// === Test setup helpers ===
Expand Down Expand Up @@ -418,7 +419,7 @@ func TestClient_GetProfile(t *testing.T) {
{
name: "amizone client is not logged in and returns the login page",
client: nonLoggedInClient,
setup: func(g *WithT) {
setup: func(_ *WithT) {
_ = mock.GockRegisterUnauthenticatedGet("/IDCard")
},
profileMatcher: func(g *WithT, profile *models.Profile) {
Expand Down
28 changes: 15 additions & 13 deletions amizone/internal/mock/testdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ func (f File) Open() (fs.File, error) {

// Constants for file paths in the filesystem embedded filesystem.
const (
DiaryEventsNone File = "testdata/diary_events_none.json"
DiaryEventsJSON File = "testdata/diary_events.json"
DiaryEventsSmallJSON File = "testdata/diary_events_small.json"
ExaminationSchedule File = "testdata/examination_schedule.html"
HomePageLoggedIn File = "testdata/home_page_logged_in.html"
LoginPage File = "testdata/login_page.html"
CoursesPage File = "testdata/my_courses.html"
CoursesPageSemWise File = "testdata/courses_semwise.html"
IDCardPage File = "testdata/id_card_page.html"
WifiPage File = "testdata/wifi_mac_registration.html"
WifiPageOneSlotPopulated File = "testdata/wifi_mac_registration_one_empty.html"
FacultyPage File = "testdata/faculty_page.html"
DiaryEventsNone File = "testdata/diary_events_none.json"
DiaryEventsJSON File = "testdata/diary_events.json"
DiaryEventsSmallJSON File = "testdata/diary_events_small.json"
ExaminationSchedule File = "testdata/examination_schedule.html"
ExaminationScheduleWithLocation File = "testdata/examination_schedule_exam_room.html"
HomePageLoggedIn File = "testdata/home_page_logged_in.html"
LoginPage File = "testdata/login_page.html"
CoursesPage File = "testdata/my_courses.html"
CoursesPageSemWise File = "testdata/courses_semwise.html"
IDCardPage File = "testdata/id_card_page.html"
WifiPage File = "testdata/wifi_mac_registration.html"
WifiPageOneSlotPopulated File = "testdata/wifi_mac_registration_one_empty.html"
FacultyPage File = "testdata/faculty_page.html"
)

type ExpectedJSON string
Expand All @@ -41,5 +42,6 @@ func (f ExpectedJSON) Open() (fs.File, error) {
}

const (
ExpectedFacultyFeedbackSpec ExpectedJSON = "testdata/expected__faculty_feedback_spec.json"
ExpectedFacultyFeedbackSpec ExpectedJSON = "testdata/expected__faculty_feedback_spec.json"
ExpectedExamScheduleWithRoom ExpectedJSON = "testdata/expected__exam_schedule_with_room.json"
)
115 changes: 115 additions & 0 deletions amizone/internal/mock/testdata/examination_schedule_exam_room.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
<!-- TODO: sanitise -->
<div class="main-content-inner">
<div class="breadcrumbs" id="breadcrumbs">

<ul class="breadcrumb">
<li><i class="ace-icon fa fa-home home-icon"></i><a href="/home">Home</a> </li>
<li class="active">Examination</li>
<li class="active">Examination Schedule</li>
</ul>
<!-- /.breadcrumb -->
<!-- /.nav-search -->
</div>
<div class="page-content">
<div class="page-header">
<h1>
END SEMESTER EXAMINATION , Even SEMESTER 2022 (Academic Session 2021-2022)
</h1>
</div>


<div class="row">
<div class="col-xs-12">
<div class="panel-group" id="accordion">
<div class="panel panel-default">

<div id="no-more-tables">
<table class="table table-bordered table-condensed">
<thead class="cf">
<tr>
<th><strong>Course Code</strong></th>
<th><strong>Course Title</strong></th>
<th><strong>Exam Date</strong></th>
<th><strong>Exam Time</strong></th>
<th><strong>Details</strong></th>
</tr>
</thead>
<tbody>


<tr>
<td data-title="Course Code">FREN144</td>
<td data-title="Course Title">French Through Communicative Approach</td>
<td data-title="Exam Date">11/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : MCQ</b> Regular [Sem - 4 ] </td>
</tr>
<tr>
<td data-title="Course Code">BS207</td>
<td data-title="Course Title">Self-Reliance and Socialization</td>
<td data-title="Exam Date">13/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : MCQ</b> Regular [Sem - 4 ] </td>
</tr>
<tr>
<td data-title="Course Code">MATH242</td>
<td data-title="Course Title">Applied Mathematics-IV</td>
<td data-title="Exam Date">14/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : Regular</b> Regular [Sem - 4 ] </td>
</tr>
<tr>
<td data-title="Course Code">CSE208</td>
<td data-title="Course Title">Discrete Mathematical Structures</td>
<td data-title="Exam Date">17/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : Regular</b> Regular [Sem - 4 ] <b style="color:red"><br/>Location :- Block - E3 , Second Floor , Room No -211<br/>Campus Entry :- From Gate No : 4</b></td>
</tr>
<tr>
<td data-title="Course Code">CSE202</td>
<td data-title="Course Title">Operating System</td>
<td data-title="Exam Date">18/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : Regular</b> Regular [Sem - 4 ] <b style="color:red"><br/>Location :- Block - E1 , Fourth Floor , Room No -412<br/>Campus Entry :- From Gate No : 4</b></td>
</tr>
<tr>
<td data-title="Course Code">IT201</td>
<td data-title="Course Title">Java Programming</td>
<td data-title="Exam Date">20/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : Regular</b> Regular [Sem - 4 ] </td>
</tr>
<tr>
<td data-title="Course Code">CSE204</td>
<td data-title="Course Title">Theory of Computation</td>
<td data-title="Exam Date">23/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : Regular</b> Regular [Sem - 4 ] </td>
</tr>
<tr>
<td data-title="Course Code">MATS201</td>
<td data-title="Course Title">Material Science</td>
<td data-title="Exam Date">25/05/2022</td>
<td data-title="Time">10:00</td>
<td data-title="Paper Type"><b>Exam Mode : Regular</b> Regular [Sem - 4 ] </td>
</tr>


</tbody>
</table>
</div>

</div>
</div>
</div>
</div>

<div class="page-header">

<h1><small><b></b></small></h1>

</div>
</div>

</div>

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{
"Title": "End Semester Examination , Even Semester 2022 (Academic Session 2021-2022)",
"Exams": [
{
"Course": {
"Code": "FREN144",
"Name": "French Through Communicative Approach"
},
"Time": "2022-05-11T10:00:00Z",
"Mode": "MCQ",
"Location": ""
},
{
"Course": {
"Code": "BS207",
"Name": "Self-Reliance and Socialization"
},
"Time": "2022-05-13T10:00:00Z",
"Mode": "MCQ",
"Location": ""
},
{
"Course": {
"Code": "MATH242",
"Name": "Applied Mathematics-IV"
},
"Time": "2022-05-14T10:00:00Z",
"Mode": "Regular",
"Location": ""
},
{
"Course": {
"Code": "CSE208",
"Name": "Discrete Mathematical Structures"
},
"Time": "2022-05-17T10:00:00Z",
"Mode": "Regular",
"Location": "Block - E3 , Second Floor , Room No -211"
},
{
"Course": {
"Code": "CSE202",
"Name": "Operating System"
},
"Time": "2022-05-18T10:00:00Z",
"Mode": "Regular",
"Location": "Block - E1 , Fourth Floor , Room No -412"
},
{
"Course": {
"Code": "IT201",
"Name": "Java Programming"
},
"Time": "2022-05-20T10:00:00Z",
"Mode": "Regular",
"Location": ""
},
{
"Course": {
"Code": "CSE204",
"Name": "Theory of Computation"
},
"Time": "2022-05-23T10:00:00Z",
"Mode": "Regular",
"Location": ""
},
{
"Course": {
"Code": "MATS201",
"Name": "Material Science"
},
"Time": "2022-05-25T10:00:00Z",
"Mode": "Regular",
"Location": ""
}
]
}
Loading

0 comments on commit 387809f

Please sign in to comment.